问题
I'm trying to match a string (using a Perl regex) only if it doesn't start with "abc:" or "defg:", but I can't seem to find out how. I've tried something like
^(?:(?!abc:)|(?!defg:))
回答1:
Lookahead (?=foo), (?!foo) and lookbehind (?<=foo), (?<!foo) do not consume any characters.
You can:
^(?!abc:)(?!defg:)
or
^(?!defg:)(?!abc:)
The order does not make a difference.
回答2:
Try doing this :
^(?!(?:abc|defg):)
回答3:
… or could have dropped the alternation from the original expression:
^(?:(?!abc:)(?!defg:))
回答4:
^(?!abc:|defg:)\s*\w+
use this regex. this will avoid line start with "abc:" and "defg:" as you want.
回答5:
This will do the task :
^(?!(defg|abc):).*
回答6:
^(?:(?!abc:|defg:).)*$
Try this.See demo.
http://regex101.com/r/hQ9xT1/18
回答7:
Could you please try this:
use strict;
use warnings;
use Cwd;
while(<DATA>)
{
my $line=$_;
print $line unless($line=~m/^(abc|defg*)/m);
}
__DATA__
ebc this is testing ebc
dbc this is testing dbc
defg this is testing defg
abc this is testing abc
defg this is testing defg
来源:https://stackoverflow.com/questions/27179991/regex-matching-multiple-negative-lookahead