0

I'm wondering how to match all words that contain a given digit?

I've looked at O'Reilly Regular Expressions Cookbook, but failed to get the idea.

Case 1. Some characters are in front of the digit.(Solved)

\b(\d+[^\s]+)\b

time=>123, address~4321

Case 2. Some characters are followed by digit.(Solved)

\b(\d+[^\s]+)\b

1234<=range, 321=>location

Case 3. Some characters are forth digit.

time~1325@range, address*4321%location

Case 4. Duplicate in a single line of Case1 or Case2 or Case3.

firstTime=12   secondTime=34 
429923<=firstRange                353534=>secondRange
A12345alpha      B9876beta
bitmapdata.com
  • 9,572
  • 5
  • 35
  • 43

3 Answers3

0

Since you have defined a word to be any string that does not contain a white space, you might want to use something like this:

([^\s]*\d+.[^\s]*)\m

The \m flag should make the regex work across multiple lines.

npinti
  • 51,780
  • 5
  • 72
  • 96
  • @gpojd: Yeah, I could do it like that as well. I guess that yours is another way of getting the job done. I have modified my answer. – npinti Dec 28 '11 at 21:10
  • @gpojd it will fail to match `A1B2C3` for instance – fge Dec 28 '11 at 21:17
  • @fge, his original answer was identical to what I suggested. I only replaced `[^\s]` with `\S` for clarity. – gpojd Dec 28 '11 at 21:19
0

Try and match (\S*(?:\d\S*)*) on your input. Again the normal* (special normal*)* pattern, with normal being \S (anything but a space character) and special being \d+ (one or more digits):

fg@erwin ~ $ perl -ne 'print "Match: -->$_<--\n" foreach m/(\S*(?:\d+\S*))/gm' <<EOF
> firstTime=12   secondTime=34 
> 429923<=firstRange                353534=>secondRange
> A12345alpha      B9876beta
> EOF
Match: -->firstTime=12<--
Match: -->secondTime=34<--
Match: -->429923<=firstRange<--
Match: -->353534=>secondRange<--
Match: -->A12345alpha<--
Match: -->B9876beta<--

Depending on the regex engine you use, you may even use possessive quantifiers all along, or an atomic group to accelerate matching.

And if you want a book --> http://regex.info

fge
  • 119,121
  • 33
  • 254
  • 329
0

Here is a regex that will match each "word" containing the digit "2".

\S*?2\S*

Change the 2 to any digit sequence you wish to match. If you wish to match Words containing a 2, 5 or 7, then use a character class:

\S*?[257]\S*
ridgerunner
  • 33,777
  • 5
  • 57
  • 69