Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex: How to match a string that is not only numbers

Tags:

regex

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.

like image 385
Svish Avatar asked Sep 30 '10 13:09

Svish


People also ask

How would you match any character that is not a digit in regular expressions?

In regex, the uppercase metacharacter is always the inverse of the lowercase counterpart. \d (digit) matches any single digit (same as [0-9] ). The uppercase counterpart \D (non-digit) matches any single character that is not a digit (same as [^0-9] ).

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

What does regex 0 * 1 * 0 * 1 * Mean?

Basically (0+1)* mathes any sequence of ones and zeroes. So, in your example (0+1)*1(0+1)* should match any sequence that has 1. It would not match 000 , but it would match 010 , 1 , 111 etc. (0+1) means 0 OR 1.


1 Answers

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

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

like image 143
Mike Cheel Avatar answered Sep 22 '22 15:09

Mike Cheel