9

Just for curiosity.

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

Alan Moore
  • 73,866
  • 12
  • 100
  • 156
Gill Bates
  • 14,330
  • 23
  • 70
  • 138
  • 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

15

Yes.

Here are a few examples.

.^
$.
(?!)

Naturally, there are an infinite number of such expressions.

Kendall Frey
  • 43,130
  • 20
  • 110
  • 148
2

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

$x^
mvp
  • 111,019
  • 13
  • 122
  • 148
0

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

René Höhle
  • 26,716
  • 22
  • 73
  • 82
sirezekiel
  • 126
  • 3