Is there a Regular Expression that will never matc

2020-06-28 00:48发布

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 "").

标签: c# .net regex
3条回答
在下西门庆
2楼-- · 2020-06-28 01:28

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.

查看更多
Root(大扎)
3楼-- · 2020-06-28 01:40

Just as you can match any characters with [\s\S], you can match no characters with [^\s\S] (or [^\w\W], etc).

查看更多
乱世女痞
4楼-- · 2020-06-28 01:45

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.

查看更多
登录 后发表回答