How to set up REGEX that doesn't match anything?
Asked Answered
A

3

11

Just for curiosity.

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

Apeldoorn answered 2/1, 2013 at 0:15 Comment(6)
With true regular languages, technically the empty set is a regular expression.Antiperiodic
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 anythingNils
@Nils Exactly! (I wrote this to avoid limitation)Apeldoorn
@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?Rigdon
@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.Nils
@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.Rigdon
I
17

Yes.

Here are a few examples.

.^
$.
(?!)

Naturally, there are an infinite number of such expressions.

Ida answered 2/1, 2013 at 0:25 Comment(2)
@Pterodactyl Certainly. Example: .^, ..^, ...^, etc.Ida
About performance of the examples in Javascript / PHP : https://mcmap.net/q/25797/-a-regex-that-will-never-be-matched-by-anythingFox
N
2

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

$x^
Nils answered 2/1, 2013 at 0:25 Comment(5)
I'm pretty sure it will match an empty string.Ida
Well, I tried Perl flavor, and it does not match empty stringNils
let us continue this discussion in chatIda
@GillBates Wow, that is certainly strange. Which engine are you using? .NET does match, but JavaScript doesn't.Ida
@Gill Bates - its about '$^' versionApeldoorn
M
0

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

Multiangular answered 2/1, 2013 at 0:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.