I need to extract words that contain digits.
ex:-
Input - 3909B Witmer Road. Niagara Falls. NY 14305
Output - 3909B and 14305
Use this regex:
\w*\d\w*
See it here in action: http://regexr.com?2vqui
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.
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.
}
The basic expression should be:
(?<=^| )(?=[^ ]*\d)[^ ]+
(\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;
}