Regex for "not any words(lines) preceeded by spaces"

M

Mark Clements

Edward said:
Hi,

[^\s.] or [^\s[a-zA-z]] doesn't seem to work.
Anything wrong with my regex?
[^\s.] will match non-whitespace or non-anything (^ inside a character
class negates the class). I'll assume that you are trying to anchor to
start-of line, though your spec doesn't mention it. You spec refers to
spaces and not whitespace, though this time I'll assume you mean spaces.

How about:
^ +\W+

though without any test data or expected results it's difficult to see
exactly what you're after.

Mark
 
G

Gunnar Hjalmarsson

Mark said:
[^\s.] will match non-whitespace or non-anything

No. A dot is not special within brackets. It matches any
non-whitespace character except a dot.
 
A

Anno Siegel

Edward Wijaya said:
Hi,

[^\s.] or [^\s[a-zA-z]] doesn't seem to work.
Anything wrong with my regex?

Hard to say before *you* say what exactly you expect it to do. The
statement in your subject is ambiguous (and you should have re-stated
it in the body of your text).

Meanwhile it would be a good idea to acquaint yourself with the
function of character classes "[...]" in regular expressions.
Those you are using above don't make much sense. In particular
you don't seem to understand that a character class matches a
single character, not many. Also, "^" has a different meaning
in character classes (negation) and outside (anchor to the beginning
the string).

Anno
 
A

Anno Siegel

[please don't to-post]
This is the example of the input (verbatim).

AGATTAGT
GCATTAGT
AGGGTAGT
GGAGTAGG
A C G T Info Ptn Pattern
TOTAL INFORMATION CONTENT


I just want to remove the last two lines (that comes
with spaces before it).

grep /^\S/, @input;

[fullquote snipped]

Anno
 
E

Edward Wijaya

Hi,

[^\s.] or [^\s[a-zA-z]] doesn't seem to work.
Anything wrong with my regex?

Thanks.

Regards
Edward WIJAYA
SINGAPORE
 
S

Sundaram Ramasamy

[please don't to-post]
This is the example of the input (verbatim).

AGATTAGT
GCATTAGT
AGGGTAGT
GGAGTAGG
A C G T Info Ptn Pattern
TOTAL INFORMATION CONTENT


I just want to remove the last two lines (that comes
with spaces before it).

grep /^\S/, @input;

[fullquote snipped]

Anno

Here is another method.

perl -ne 'print if /^[A-Z]+/' a.out


perl -ne 'print if /^\w+/' a.out

-sundaram
 
P

Paul Lalli

[please don't to-post]
This is the example of the input (verbatim).

AGATTAGT
GCATTAGT
AGGGTAGT
GGAGTAGG
A C G T Info Ptn Pattern
TOTAL INFORMATION CONTENT


I just want to remove the last two lines (that comes
with spaces before it).

Here is another method.

perl -ne 'print if /^[A-Z]+/' a.out


perl -ne 'print if /^\w+/' a.out

Neither of those meet the specs the OP specified. The requirements were
to print lines that do not begin with spaces. Your methods print only
those lines that begin with Capital letters and letters, numbers, &
underscores (respectively). Granted, the input he chose to show contains
only capital letters as the first non-whitespace. That does not mean,
however, that *all* input the OP will use meets that format, nor should it
be assumed his input will never change.

perl -ne 'print if /^\S/' a.out

Paul Lalli
 
E

Edward Wijaya

Hi,
This is the example of the input (verbatim).

AGATTAGT
GCATTAGT
AGGGTAGT
GGAGTAGG
A C G T Info Ptn Pattern
TOTAL INFORMATION CONTENT


I just want to remove the last two lines (that comes
with spaces before it).

Regards
Edward WIJAYA


Edward Wijaya said:
Hi,

[^\s.] or [^\s[a-zA-z]] doesn't seem to work.
Anything wrong with my regex?

Hard to say before *you* say what exactly you expect it to do. The
statement in your subject is ambiguous (and you should have re-stated
it in the body of your text).

Meanwhile it would be a good idea to acquaint yourself with the
function of character classes "[...]" in regular expressions.
Those you are using above don't make much sense. In particular
you don't seem to understand that a character class matches a
single character, not many. Also, "^" has a different meaning
in character classes (negation) and outside (anchor to the beginning
the string).

Anno
 
J

Josef Moellers

Paul said:
[please don't to-post]


This is the example of the input (verbatim).

AGATTAGT
GCATTAGT
AGGGTAGT
GGAGTAGG
A C G T Info Ptn Pattern
TOTAL INFORMATION CONTENT


I just want to remove the last two lines (that comes
with spaces before it).

Here is another method.

perl -ne 'print if /^[A-Z]+/' a.out


perl -ne 'print if /^\w+/' a.out


Neither of those meet the specs the OP specified.
perl -ne 'print if /^\S/' a.out

Nitpicking (and showing that TMTOWTDI):
Your solution does not meet the specs of the OP either:

perl -ne 'print unless /^\s+/' file

!!Josef B-{)
 
J

Joe Smith

Josef said:
Paul Lalli wrote:

Nitpicking (and showing that TMTOWTDI):
Your solution does not meet the specs of the OP either:

perl -ne 'print unless /^\s+/' file

There is no difference in output between
perl -ne 'print unless /^\s+/' file
and
perl -ne 'print unless /^\s/' file

Since the latter is the same as
perl -ne 'print if /^\S/' file
so why do you say it 'does not meet the specs'?

-Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top