tags:

views:

85

answers:

5

Is it possible to write a regular expression that matches all strings that does not only contain numbers? If we have these strings:

  • abc
  • a4c
  • 4bc
  • ab4
  • 123

It should match the four first, but not the last one. I have tried fiddling around in RegexBuddy with lookaheads and stuff, but I can't seem to figure it out.

A: 
/^\d*[a-z][a-z\d]*$/

May be a digit at the beginning, then at least one letter, then letters or digits

how
+3  A: 

I would do:

/[^0-9]/

That means that there has to be at least one character that is not a number, but the rest of the string could be anything.

jjnguy
You could simplify that to /[^0-9]/
PP
@PP, I am not exactly sure of general regular expression syntax, but I assume that the `/` means "any character"?
jjnguy
@Justin: it denotes start and end of regex. not "any character".
SilentGhost
This would just match a single character wouldn't it? And if the string contained a single digit, it would fail.
Svish
+5  A: 

Unless I am missing something, I think the most concise regex is...

/\D/

...or in other words, is there a not-digit in the string?

Daniel Standage
this will fail if there is a digit in the string as far as I can see
Svish
@Svish: your statement makes no sense.
SilentGhost
@Svish, maybe you should actually try it out before saying something. It is much more concise than the answer you selected, and it does work. It will not fail if there are digits in the string, it will only fail if it cannot find a "not-digit", which is exactly what you asked.
Daniel Standage
@Daniel: I did. And this does not match a string that does not contain only numbers. It matches a single letter that is not a digit. If I put that between a ^ and a $ for matching a string, it matches only the first of my test strings.
Svish
A: 

(?!^\d+$)^.+$

This says lookahead for lines that do not contain all digits and match the entire line.

Mike Cheel
This is basically what I ended up with :)
Svish
So...basically you came up with a complicated expression for /\D/.
Daniel Standage
Well no \D is different than mine because it only matches non digits. Mine returns matches that include the entire string when a match is made while just \D could be multiple matches per line. My regex returns 4 matches while \D returns 17 (for each non-digit).
Mike Cheel
@Mike Cheel I see. If that's what Svish was looking for, it definitely wasn't clear from his question. I see how that could be advantageous though.
Daniel Standage
@Daniel, how was that not clear from my question? I even had 5 examples where I marked the strings I wanted matched...
Svish
I think I finally understand the confusion. The answer I gave assumed that you were checking each of these strings separately. Indeed before answering your question, I wrote a script printed each string that matched the regular expression, which gave the output you requested. But it appears you wanted a regex that would pull out each of these substrings individually from a larger string. Based on the responses, I was not the only one that thought this.
Daniel Standage
A: 

Since you said "match", not just validate, the following regex will match correctly

\b.*[a-zA-Z]+.*\b

Passing Tests:

abc
a4c
4bc
ab4
1b1
11b
b11

Failing Tests:

123
Chad