Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PCRE Regular Expression with NOT Operation

Tags:

regex

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.

like image 847
JamesHoux Avatar asked Dec 17 '12 05:12

JamesHoux


People also ask

What is ?: In regex?

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.

How do I not match a character in regex?

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.

What does * do in regex?

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.

What does PCRE matching do?

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.


1 Answers

Yes, they're called lookahead negative assertions e.g. some_re(?!not_on_my_watch)

You might also be interested in:

  • lookbehind negative assertions, (?<!not_even_before_my_watch)
  • and negative character classes [^\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:

  • a lot of trial and error,
  • fully understanding the greedyness modifier usage .*?
  • start considering weird constructs like once-only subpatterns and conditional subpatterns
like image 107
ZJR Avatar answered Oct 07 '22 20:10

ZJR