Sort of a two part question:
- Is there any theoretical regular expression that will never match any string (using general syntax without any fancy stuff provided by modern regular expression matchers)?
- Is there a simple way to use C#'s Regex syntax to create a regex that will never match any string (this time, all the fancy stuff is included)?
NOTE: I am not referring to matching the empty string (that would be easy, just ""
).
Without multi-line mode, the end doesn't usually tend to appear before the beginning:
Or more simply, again without multi-line mode:
With lookarounds, you can do all kinds of contradictory stuff:
This forces a character to be two different things at once, which is of course impossible.
Just as you can match any characters with
[\s\S]
, you can match no characters with[^\s\S]
(or[^\w\W]
, etc).You could use contradictory lookbehinds, for example
Here
\w
will match any word character and the lookbehind(?<!\w)
will make sure that the last character was not a word.