Parse Phone Number into component parts

后端 未结 6 1219
太阳男子
太阳男子 2020-12-31 11:27

I need a well tested Regular Expression (.net style preferred), or some other simple bit of code that will parse a USA/CA phone number into component parts, so:

6条回答
  •  被撕碎了的回忆
    2020-12-31 12:09

    This regex works exactly as you want with your examples:

    Regex regexObj = new Regex(@"\(?(?[0-9]{3})\)?[-. ]?(?[0-9]{3})[-. ]*?(?[0-9]{4})[-. x]?(?[0-9]{3})");
    Match matchResult = regexObj.Match("1 (303) 555 -1234-122");
    
    // Now you have the results in groups 
    matchResult.Groups["AreaCode"];
    matchResult.Groups["Exchange"];
    matchResult.Groups["Suffix"];
    matchResult.Groups["Extension"];
    

提交回复
热议问题