9

Just for curiosity.

Is it possible to create a regular expression that will not match any string, including an empty string?

Alan Moore
  • 71,299
  • 12
  • 93
  • 154
Gill Bates
  • 11,140
  • 22
  • 64
  • 134
  • 3
    With [true regular languages](http://en.wikipedia.org/wiki/Regular_expression#Patterns_for_non--regular_languages), technically the empty set is a [regular expression](http://en.wikipedia.org/wiki/Regular_expression#Formal_language_theory). – Oliver Charlesworth Jan 02 '13 at 00:21
  • I guess it is the same point as having `NULL`, `undef` or `None`: sometimes you want to have compiled regex as starting value that never matches anything – mvp Jan 02 '13 at 00:46
  • @mvp Exactly! (I wrote this to avoid limitation) – Gill Bates Jan 02 '13 at 00:53
  • @GillBates, I removed your remark about the closure from the question. It should be a comment. You may be right that it shouldn't have been closed, but you got your answer, so it doesn't really matter, does it? – Mischa Jan 02 '13 at 01:03
  • @Mischa: this is rather strange attitude. If you think that it should not have been closed, question is clear, good answer exists, why you close it as `not a real question`? If may not matter for this user right now, but it WILL matter to other users who will read this question later. – mvp Jan 02 '13 at 01:15
  • @mvp, once you vote to close, you cannot undo it. I had already voted to close, but later when I read all comments and answers I thought I shouldn't have. – Mischa Jan 02 '13 at 09:59

3 Answers3

14

Yes.

Here are a few examples.

.^
$.
(?!)

Naturally, there are an infinite number of such expressions.

Kendall Frey
  • 41,292
  • 18
  • 105
  • 145
2

This regex should never match anything (provided you do not use single-line or multi-line modifiers):

$x^
mvp
  • 102,692
  • 13
  • 114
  • 144
0

How about /^$x/. When I try it with ruby, it seems to work.

René Höhle
  • 25,733
  • 22
  • 69
  • 78
sirezekiel
  • 26
  • 3