develooper Front page | perl.beginners | Postings from November 2009

Re: Regular expressions question

Thread Previous | Thread Next
From:
Dermot
Date:
November 18, 2009 07:42
Subject:
Re: Regular expressions question
Message ID:
9c7446e70911180742k58b1838fn141618df6a69deeb@mail.gmail.com
2009/11/17 mangled_us@yahoo.com <mangled_us@yahoo.com>:
> Hi,

Hello,


> Can anyone tell me hoq to write a regular expression which matches
> anything _except_ a litteral string ?
>
> For instance, I want to match any line which does not begin with
> Nomatch.  So in the following :
>
> Line1 xxxx
> Line2 yyyy
> Nomatch zzzz
> Line3 aaaa
> Line 4 bbbb
>
> I would match every line except the one containing "Nomatch zzzz"


You would negate the pattern. Something like this:

#!/usr/bin/perl


use strict;
use warnings;

while (<DATA>) {
        print if ! /^Nomatch/;
}

__DATA__
Line1 xxxx
Line2 yyyy
Nomatch zzzz
Line3 aaaa
Line 4 bbbb
~

Output:
Line1 xxxx
Line2 yyyy
Line3 aaaa
Line 4 bbbb

see
perldoc perlop          #Logical-Not
and
perldoc perlsyn
and of course
perldoc perlrequick


HTH,
Dp.

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About