Regex to *not* match any characters
Solution 1:
The ^
character doesn't mean "not" except inside a character class ([]
). If you want to not match anything, you could use a negative lookahead that matches anything: (?!.*)
.
Solution 2:
A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B
.
It's simply impossible for this regex to match, since it's a contradiction.
References
-
regular-expressions.info\Word Boundaries
-
\B
is the negated version of\b
.\B
matches at every position where\b
does not.
-
Solution 3:
Another very well supported and fast pattern that would fail to match anything that is guaranteed to be constant time:
$unmatchable pattern
$anything goes here
etc.
$
of course indicates the end-of-line. No characters could possibly go after $
so no further state transitions could possibly be made. The additional advantage are that your pattern is intuitive, self-descriptive and readable as well!