Is it possible to use something equivalent to a NOT operation in a regular expression so as to match the string only if it does NOT contain a certain character in a given position?
I see the meta characters say ^ can mean "negate a class" but I am having trouble finding examples of its use. And that particular character is ambiguous with the start of a string so its a bit confusing as well.
It indicates that the subpattern is a non-capture subpattern. That means whatever is matched in (?:\w+\s) , even though it's enclosed by () it won't appear in the list of matches, only (\w+) will.
There's two ways to say "don't match": character ranges, and zero-width negative lookahead/lookbehind. Also, a correction for you: * , ? and + do not actually match anything. They are repetition operators, and always follow a matching operator.
The Match-zero-or-more Operator ( * ) This operator repeats the smallest possible preceding regular expression as many times as necessary (including zero) to match the pattern. `*' represents this operator. For example, `o*' matches any string made up of zero or more `o' s.
PCRE tries to match Perl syntax and semantics as closely as it can. PCRE also supports some alternative regular expression syntax (which does not conflict with the Perl syntax) in order to provide some compatibility with regular expressions in Python, . NET, and Oniguruma.
Yes, they're called lookahead negative assertions e.g. some_re(?!not_on_my_watch)
You might also be interested in:
(?<!not_even_before_my_watch)
[^\d\sa-z]
You might also want to check the relative pcre/php documentation for assertions.
To see if you find some other ones interesting.
PLEASE NOTE: Negative assertions may will match against your will, in positions in the string, you didn't think of. Be prepared for:
.*?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With