Regex matching multiple negative lookahead

2019-02-16 15:46发布

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

7条回答
我命由我不由天
2楼-- · 2019-02-16 16:03

This will do the task :

^(?!(defg|abc):).*
查看更多
萌系小妹纸
3楼-- · 2019-02-16 16:04

Try doing this :

^(?!(?:abc|defg):)
查看更多
Anthone
4楼-- · 2019-02-16 16:04

… or could have dropped the alternation from the original expression:

^(?:(?!abc:)(?!defg:))
查看更多
仙女界的扛把子
5楼-- · 2019-02-16 16:06

^(?!abc:|defg:)\s*\w+

use this regex. this will avoid line start with "abc:" and "defg:" as you want.

查看更多
何必那么认真
6楼-- · 2019-02-16 16:11
^(?:(?!abc:|defg:).)*$

Try this.See demo.

http://regex101.com/r/hQ9xT1/18

查看更多
放荡不羁爱自由
7楼-- · 2019-02-16 16:13

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
查看更多
登录 后发表回答