On Wed, Nov 18, 2009 at 5:05 PM, Thomas Bätzler <[email protected]>wrote:
> Hi, > > Dermot <[email protected]> suggested: > > 2009/11/17 [email protected] <[email protected]>: > > > > 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 > > -- > To unsubscribe, e-mail: [email protected] > For additional commands, e-mail: [email protected] > http://learn.perl.org/ > > > Look ahead notation works only on relatively recent versions of Perl, if your environment contains things like HP-UX that ships with a decades old version of Perl 5.005 I believe it is (depending on the version of HP-UX of course) you might get in trouble. I would therefore not use it or make the script explicitly require 5.6 or higher just in case. Regards, Rob
