Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex: match any word with numbers

Tags:

regex

I checked on stackoverflow already but didn't find a solution I could use. I need a regular expression to match any word (by word I mean anything between full spaces) that contains numbers. It can be alphanumeric AB12354KFJKL, or dates 11/01/2014, or numbers with hyphens in the middle, 123-489-568, or just plain normal numbers 123456789 - but it can't match anything without numbers. Thanks,

Better example of what I want (in bold) in a sample text:

ABC1 ABC 23-4787 ABCD 4578 ABCD 11/01/2014 ABREKF

like image 475
myeong Avatar asked Jan 11 '17 10:01

myeong


People also ask

Can you use regex with numbers?

Since regular expressions work with text, a regular expression engine treats 0 as a single character, and 255 as three characters. To match all characters from 0 to 255, we'll need a regex that matches between one and three characters. The regex [0-9] matches single-digit numbers 0 to 9.

What does '$' mean in regex?

$ means "Match the end of the string" (the position after the last character in the string). Both are called anchors and ensure that the entire string is matched instead of just a substring.

How do I match a number in regex?

To match any number from 0 to 9 we use \d in regex. It will match any single digit number from 0 to 9. \d means [0-9] or match any number from 0 to 9. Instead of writing 0123456789 the shorthand version is [0-9] where [] is used for character range.

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

There must be something better, but I think this should work:

\S*\d+\S*

\S* - Zero or more non-whitespace characters

\d+ - One or more digits

\S* - Zero or more non-whitespace characters

like image 71
Anderson Pimentel Avatar answered Oct 17 '22 23:10

Anderson Pimentel