Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a Regular Expression that will never match any string? [closed]

Tags:

c#

.net

regex

Sort of a two part question:

  1. 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)?
  2. 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 "").

like image 784
Matt Avatar asked Dec 18 '25 07:12

Matt


2 Answers

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:

(?=a)(?=b)

This forces a character to be two different things at once, which is of course impossible.

like image 60
phant0m Avatar answered Dec 19 '25 21:12

phant0m


You could use contradictory lookbehinds, for example

\w(?<!\w)

Here \w will match any word character and the lookbehind (?<!\w) will make sure that the last character was not a word.

like image 27
David Pärsson Avatar answered Dec 19 '25 22:12

David Pärsson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!