Is is possible to construct a regular expression that rejects all input strings?
Why would you even want that? Wouldn't a simple if statment do the trick? Something along the lines of:
if ( inputString != "" ) doSomething ()
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:
(?<!)
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!
(?=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).
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.
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.
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".
[^]+ 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 ;)
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.