tags:

views:

834

answers:

11

Is is possible to construct a regular expression that rejects all input strings?

A: 

EDIT: [^\n\r\w\s]

Unkwntech
Wouldn't that accept all input strings? I think you might want to negate that statement.
Thomas Owens
This is the exact opposite of what I'm looking for. Your regular expression aceepts everything, what I need is a regular expression that rejects everything.
Kasper
Unkwntech, your regex will match punctuation symbols, etc.
aku
+1  A: 

Why would you even want that? Wouldn't a simple if statment do the trick? Something along the lines of:

if ( inputString != "" ) doSomething ()

Jan Hancic
+6  A: 

Probably this:

[^\w\W]

\w - word character (letter, digit, etc)
\W - opposite of \w

[^\w\W] - should always fail, because any character should belong to one of the character classes - \w or \W

Another snippets:

$.^

$ - assert position at the end of the string
^ - assert position at the start of the line
. - any char

(?#it's just a comment inside of empty regex)

Empty lookahead/behind should work:

(?<!)
aku
What about the empty string?
Chris Conway
Chris Conway, are you joking ? :)
aku
No. These regex's look to match any non-empty string. What about the empty string? That's also a possible input.
Chris Conway
Chris, this regex matches *1* character from [^\w\W] character class. *Single* character.
aku
Same with $.^ - it requires a *single* character. Emptry regex (i.e. comment only) and empty lookarounds probably depend on regex engine, but work in java, c# and other mainstream regex processors.
aku
I don't think you understand me: the questions asks for a regex that matches *all* inputs. Your regexs match all *non-empty* inputs (or, more precisely, any single character of any non-empty input). What about matching all empty *and* non-empty inputs?
Chris Conway
Chris, did you ever read the question? Kasper asks for "regular expression that rejects all input strings". Now, what do you think, will an empty string be matched by regex that requires 1 character?
aku
aku, sorry I got turned around there: I meant, do your regex's *reject* the empty string? Your first example doesn't.
Chris Conway
Chris, I have no idea what regex engine you use. If regex *requires 1 char* how can an empty string be ever matched? If you want to add useful information, add a comment "I've tried in in <Your regex engine> and it didn't work. Be warned!"
aku
OK, you're right. I was confused about the semantics of [^...]: it requires a single character, which rules out the empty string (and the conjunction of \w and \W rule out any character). This trick would work with any negatable character class.
Chris Conway
+1  A: 

[^\x00-\xFF]

Erik
A: 

Well,

I am not sure if I understood, since I always thought of regular expression of a way to match strings. I would say the best shot you have is not using regex.

But, you can also use regexp that matches empty lines like ^$ or a regexp that do not match words/spaces like [^\w\s] ...

Hope it helps!

Fernando Barrocal
+3  A: 
(?=not)possible

?= is a positive lookahead. They're not supported in all regexp flavors, but in many.

The expression will look for "not", then look for "possible" starting at the same position (since lookaheads don't move forward in the string).

Henrik N
That made my day =). Well done ;)
Joseph Pecoraro
+1  A: 

To me it sounds like you're attacking a problem the wrong way, what exactly are you trying to solve?

You could do a regular expression that catches everything and negate the result.

e.g in javascript:

if (! str.match( /./ ))

but then you could just do

if (!foo)

instead, as @[jan-hani] said.

If you're looking to embed such a regex in another regex, you might be looking for $ or ^ instead, or use lookaheads like @[henrik-n] mentioned.

But as I said, this looks like a "I think I need x, but what I really need is y" problem.

asksol
A: 

It depends on what you mean by "regular expression". Do you mean regexps in a particular programming language or library? In that case the answer is probably yes, and you can refer to any of the above replies.

If you mean the regular expressions as taught in computer science classes, then the answer is no. Every regular expression matches some string. It could be the empty string, but it always matches something.

In any case, I suggest you edit the title of your question to narrow down what kinds of regular expressions you mean.

+2  A: 

One example of why such thing could possibly be needed is when you want to filter some input with regexes and you pass regex as an argument to a function.

In spirit of functional programming, for algebraic completeness, you may want some trivial primary regexes like "everything is allowed" and "nothing is allowed".

squadette
A: 

[^]+ should do it

In answer to aku's comment attached to this, I tested it with an online regex tester (http://www.regextester.com/), and so assume it works with JavaScript. I have to confess to not testing it in "real" code ;)

David Arno
I wonder, what regex engine supports this? AFAIK, [^] won't compile in most processors
aku
+1  A: 

The best standard regexs (i.e., no lookahead or back-references) that reject all inputs are (after @aku above)

.^

and

$.

These are flat contradictions: "a string with a character before its beginning" and "a string with a character after its end."

NOTE: It's possible that some regex implementations would reject these patterns as ill-formed (it's pretty easy to check that ^ comes at the beginning of a pattern and $ at the end... with a regular expression), but the few I've checked do accept them. These also won't work in implementations that allow ^ and $ to match newlines.

Chris Conway