Regex that says what NOT to match?

2019-05-23 12:23发布

I’m wondering how to match any characters except for a particular string (call it "for") in a regex.

I was thinking maybe it was something like this: [^for]* — except that that doesn’t work.

3条回答
劫难
2楼-- · 2019-05-23 12:52
^(?!for$).*$

matches any string except for.

^(?!.*for).*$

matches any string that doesn't contain for.

^(?!.*\bfor\b).*$

matches any string that doesn't contain for as a complete word, but allows words like forceps.

查看更多
你好瞎i
3楼-- · 2019-05-23 12:53

I’m sure this a dup.

One way is to start your pattern with a lookahead like this:

(?=\A(?s:(?!for).)*\z)

That can be written like this in any regex system worth bothering with:

(?x)            # turn /x mode on for commentary & spacing
(?=             # lookahead assertion; hence nonconsumptive
    \A          # beginning of string
    (?s:        # begin atomic group for later quantification
                        # enable /s mode so dot can cross lines    
        (?! for )       # lookahead negation: ain't no "for" here
        .               # but there is any one single code point
    )           # end of "for"-negated anything-dot
    *           # repeat that group zero or more times, greedily
    \z          # until we reach the very end of the string
)               # end of lookahead

Now just put that in the front of your pattern, and add whatever else you’d like afterwords. That’s how you express the logic !/for/ && ⋯ when you have to built such knowledge into the pattern.

It is similar to how you construct /foo/ && /bar/ && /glarch/ when you have to put it in a single pattern, which is

(?=\A(?s:.)*foo)(?=\A(?s:.)*bar)(?=\A(?s:.)*glarch)
查看更多
手持菜刀,她持情操
4楼-- · 2019-05-23 13:06

You can try to check whether the string matches for, and negate the result, in whatever language you use (e.g. if (not $_ =~ m/for/) in Perl)

查看更多
登录 后发表回答