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.
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.
matches any string except
for
.matches any string that doesn't contain
for
.matches any string that doesn't contain
for
as a complete word, but allows words likeforceps
.I’m sure this a dup.
One way is to start your pattern with a lookahead like this:
That can be written like this in any regex system worth bothering with:
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 isYou 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)