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

AW: Regular expressions question

Thread Previous | Thread Next
From:
Thomas Bätzler
Date:
November 18, 2009 08:06
Subject:
AW: Regular expressions question
Message ID:
E350C77ABFDBD242BDD51F5DA07905C104B55821@SONNE.gw.bringe.net
Hi,

Dermot <paikkos@googlemail.com> suggested:
> 2009/11/17 mangled_us@yahoo.com <mangled_us@yahoo.com>:

> > 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 :


> 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

One could also use a zero-with negative look-ahead assertion:

#!/usr/bin/perl -w

use strict;

while( my $line = <DATA> ){
  if( $line =~ m/^(?!Nomatch)/ ){
    print "match: $line";
  }
}

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

Cheers,
Thomas

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