Is it possible to write regular expression, evaluation of which will always fail? [duplicate]
Possibl开发者_如何学Goe 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.
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$
精彩评论