Reputation: 137
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
Upvotes: 11
Views: 30183
Reputation: 5767
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
Upvotes: 23
Reputation: 521289
Use this lookahead:
(?=\D*\d)
This asserts that the string contains any quantity of non numeric characters (\D
) followed by a single digit.
If you want to match/capture the string, then just add .*
to the regex:
(?=\D*\d).*
Reference: http://www.rexegg.com/regex-lookarounds.html
Upvotes: 0