6

I need to extract words that contain digits.

ex:-

Input - 3909B Witmer Road. Niagara Falls. NY 14305

Output - 3909B and 14305

jason
  • 236,483
  • 35
  • 423
  • 525
Dan
  • 83
  • 1
  • 4

4 Answers4

14

Use this regex:

\w*\d\w*

See it here in action: http://regexr.com?2vqui

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
1

The basic expression should be:

  1. (?<=^| )(?=[^ ]*\d)[^ ]+

    • OR -
  2. (\w*\d[\w\d]+)

And to use it in C#:

var matches = Regex.Matches(input, @"(\w*\d[\w\d]+)");

foreach (Match match in matches){
       var word = match.Value; 
}

...

var matches = Regex.Matches(input, @"(?<=^| )(?=[^ ]*\d)[^ ]+");

foreach (Match match in matches){
    var word = match.Value; 
}
Jim D'Angelo
  • 3,952
  • 3
  • 25
  • 39
Segev -CJ- Shmueli
  • 1,535
  • 14
  • 15
0

You mean you want to extract number-ey words:

var matches = Regex.Matches(input, @"\d\w*");

foreach (Match match in matches) {
    var numWord = match.Value;    // 3909B, etc.
}
Cameron
  • 96,106
  • 25
  • 196
  • 225
  • @Joseph: Correct. Whether the OP wants that or not is open to interpretation ;-) – Cameron Jan 26 '12 at 05:17
  • Thank you very much it works.. yeah for that we have to add +\w in the front. correct regex would be @"\w+\d+\w*" – Dan Jan 26 '12 at 05:27
0

This is the simplest regex I could come up with that can handle words that have a mixture of letters and digits:

(\w*\d[\w\d]+)

So this will match your desired words, plus it would match 'abc123xyz'. Try it yourself.

metatation
  • 167
  • 6