Regex to *not* match any characters

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!


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: (?!.*).


tldr; The most portable and efficient regex to never match anything is $- (end of line followed by a char)


Impossible regex

The most reliable solution is to create an impossible regex. There are many impossible regexes but not all are as good.

First you want to avoid "lookahead" solutions because some regex engines don't support it.

Then you want to make sure your "impossible regex" is efficient and won't take too much computation steps to match... nothing.

I found that $- has a constant computation time ( O(1) ) and only takes two steps to compute regardless of the size of your text (https://regex101.com/r/yjcs1Z/3).

For comparison:

  • $^ and $. both take 36 steps to compute -> O(1)
  • \b\B takes 1507 steps on my sample and increase with the number of character in your string -> O(n)

Empty regex (alternative solution)

If your regex engine accepts it, the best and simplest regex to never match anything might be: an empty regex .


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.

Tags:

Regex