Regex to extract words that contain digits

前端 未结 4 470
被撕碎了的回忆
被撕碎了的回忆 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: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; 
    }
    

提交回复
热议问题