Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Regex for "not any words(lines) preceeded by spaces"

Reply
Thread Tools

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

 
 
Mark Clements
Guest
Posts: n/a
 
      05-18-2004
Edward Wijaya wrote:
> 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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      05-18-2004
Mark Clements wrote:
> [^\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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      05-18-2004
Gunnar Hjalmarsson wrote:
> Mark Clements wrote:
>
>> [^\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.
>

er - yeah. schoolboy error
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-18-2004
Edward Wijaya <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      05-18-2004
Edward Wijaya <(E-Mail Removed)> wrote in comp.lang.perl.misc:

[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
 
Reply With Quote
 
Edward Wijaya
Guest
Posts: n/a
 
      05-18-2004
Hi,

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

Thanks.

Regards
Edward WIJAYA
SINGAPORE


--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
 
Reply With Quote
 
Sundaram Ramasamy
Guest
Posts: n/a
 
      05-18-2004
http://www.velocityreviews.com/forums/(E-Mail Removed)-berlin.de (Anno Siegel) wrote in message news:<c8d3s3$es9$(E-Mail Removed)-Berlin.DE>...
> Edward Wijaya <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>
> [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
 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      05-18-2004
On Tue, 18 May 2004, Sundaram Ramasamy wrote:

> > Edward Wijaya <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >
> > [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
 
Reply With Quote
 
Edward Wijaya
Guest
Posts: n/a
 
      05-18-2004
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


On 18 May 2004 11:01:45 GMT, Anno Siegel
<(E-Mail Removed)-berlin.de> wrote:

> Edward Wijaya <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>> 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




--
Using M2, Opera's revolutionary e-mail client: http://www.opera.com/m2/
 
Reply With Quote
 
Josef Moellers
Guest
Posts: n/a
 
      05-19-2004
Paul Lalli wrote:
> On Tue, 18 May 2004, Sundaram Ramasamy wrote:
>
>
>>>Edward Wijaya <(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>>
>>>[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-{)
--
Josef Möllers (Pinguinpfleger bei FSC)
If failure had no penalty success would not be a prize
-- T. Pratchett

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
variables preceeded with @ Thomas Girod Python 2 02-07-2006 02:58 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments