tags:

views:

196

answers:

8

I know it is quite some weird goal here but for a quick and dirty fix for one of our system we do need to not filter any input and let the corruption go into the system.

My current regex for this is "\^.*"

The problem with that is that it does not match characters as planned ... but for one match it does work. The string that make it not work is ^@jj (basically anything that has ^ ... ).

What would be the best way to not match any characters now ? I was thinking of removing the \  but only doing this will transform the "not" into a "start with" ...

A: 

Have you tried this simple regex? [^.]*

Earlz
Yep, matches everything. Should match nothing.
Erick
@Erick that's interesting. I'd like to know why...
Earlz
I don't think that `.` is special when inside a character class. This matches anything that's not a dot.
JSBangs
@Earlz good question, I tried in regex coach, it highlighted everything as weird as it can be.
Erick
`.` is treated literally inside a character class (as it doesn't really make sense to want to match everything inside one!)
Chris Smith
Fist, this matches everything because "*" means "zero or more". What you more likely want is "[^.]". But be careful, because in some regexp engines '.' doesn't match newlines (unless an appropriate flag is given); and in others, '.' may be treated specially inside [...]. So you might instead try something like [^\w\W] or [^\s\S].
Edward Loper
+11  A: 

The ^ character doesn't mean "not" except inside a character class ([]). If you want to not match anything, you could use a negative lookahead that matches anything: (?!.*).

JSBangs
+1 for negative lookahead solution.
x1a4
Seems to work ! But this construct (?! <= ) is quite weird to me, what does it means exactly ?
Erick
@Erick, read this page for info about lookaround operators: http://www.regular-expressions.info/lookaround.html.
JSBangs
@JS Bang just started reading, sounds like something quite advanced but necessary. Thanks for the solution!
Erick
This might be more efficient: /(?!a)a/ (Can't believe we're writing regexen to match nothing :))
pilcrow
@pilcrow business reasons ;-)
Erick
@Erick, ha! If I may borrow from Pascal, the business has its reasons of which reason knows nothing. :) (Of course, reason alone usually doesn't lead to profit.)
pilcrow
A: 

You want to match nothing at all? Neg lookarounds seems obvious, but can be slow, perhaps ^$ (matches empty string only) as an alternative?

annakata
A: 

^ is only not when it's in class (such as [^a-z] meaning anything but a-z). You've turned it into a literal ^ with the backslash.

What you're trying to do is [^]*, but that's not legal. You could try something like

" {10000}"

which would match exactly 10,000 spaces, if that's longer than your maximum input, it should never be matched.

You don't say what regular expression variant you're using, make sure it supports {} as a repetition count before trying this. It works in Python.
+1  A: 

Instead of trying to not match any characters, why not just match all characters? ^.*$ should do the trick. If you have to not match any characters then try ^\j$ (Assuming of course, that your regular expression engine will not throw an error when you provide it an invalid character class. If it does, try ^()$. A quick test with RegexBuddy suggests that this might work.

Sean Vieira
A: 

What about no regex (empty string)?

mbeckish
That will match everything trivially.
+12  A: 

A simple and cheap regex that will never match anything is to match against something that is simply unmatchable, for example: \b\B.

It's simply impossible for this regex to match, since it's a contradiction.

References

polygenelubricants
This is the usual solution, more widely-supported than lookaround.
bobince
OK, it's funny that there's a "usual solution" to this. =)
khedron
A: 

Eh I know this is a little late, but you could simply not read any input if the regex is empty

Adrian Regan