2

Possible Duplicate:
What regular expression can never match?

I would like to write regular expression, which will match nothing, that is corresponding to empty language. Empty language is regular, so there should be a way to write regular expression for it.

I'm particularly interested in how to write it in Java using API provided by java.util.regex package (if possible), but any other language/API will be fine. I'm curious, if something like that is possible.

To clarify it once more, I want to find regular expression EMPTY_LANG_REGEX such that

Pattern.compile(EMPTY_LANG_REGEX).matcher(s).matches()

will yield false for every string s.

I know something like a++a works, but that is kind of hack and has performance issues.

Thanks for answers.

Community
  • 1
  • 1
Andrej Herich
  • 3,246
  • 27
  • 14
  • 1
    Duplicate of http://stackoverflow.com/questions/1845078/what-regular-expression-can-never-match – Avi Feb 20 '10 at 17:19
  • +1 Avi: nice find. They got all of the suggestion in my answer apart from one: `^\b$`, so I added the missing one on that thread. This one should be closed. – Mark Byers Feb 20 '10 at 17:29

1 Answers1

2

To match no strings at all, not even then empty string:

^(?=a)b$

Another alternative that doesn't use lookaheads (doesn't work in multiline mode):

a^

One more way, using a negative character class that fails for every character:

^[^\S\s]$

For regex implementations that support matching on word boundaries, this will always fail because there won't be a word boundary inbetween the a characters in 'aa':

^a\ba$

Or simpler, and probably also fails to match in most regex implementations:

^\b$
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452