Regex to extract words that contain digits

前端 未结 4 447
被撕碎了的回忆
被撕碎了的回忆 2020-12-19 08:21

I need to extract words that contain digits.

ex:-

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

Output - 3909B and 14305

相关标签:
4条回答
  • 2020-12-19 08:26

    Use this regex:

    \w*\d\w*
    

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

    0 讨论(0)
  • 2020-12-19 08:31

    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.

    0 讨论(0)
  • 2020-12-19 08:35

    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.
    }
    
    0 讨论(0)
  • 2020-12-19 08:51

    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; 
    }
    
    0 讨论(0)
提交回复
热议问题