【问题标题】:.NET Regular Expressions - Finding, Replace.NET 正则表达式 - 查找、替换
【发布时间】:2010-02-14 08:16:38
【问题描述】:

我有一个包含地址和电话号码的字符串(美国格式;(xxx) xxx-xxxx)。例如,

1243 K. Beverly Bld. # 223
Los Angeles, CA 41124
(213) 314-3221

这是一个字符串,我需要使用正则表达式从中提取电话号码。我本可以使用字符串标记,但有可能一些无效数据也与此字符串连接。所以我认为使用正则表达式将是查找电话号码的最简单和最快的方法。找到电话号码后,我需要从输入字符串中删除。

有人可以分享一下 quick-code-sn-p 吗?

【问题讨论】:

  • C# 没有正则表达式
  • @John:我的意思是我需要在 C# 中使用正则表达式。不过,谢谢你把事情说清楚。

标签: c# .net regex


【解决方案1】:

这适用于美国的号码:

 ^                         # beginning of string, or BOL in multi-line mode
 (?:[+]?1[-. ]){0,1}       # optional calling code, not captured
 \(?                       # optional common prefix for area code, not captured
 ([2-9][0-8][0-9])?        # optional NANP-allowed area codes, captured in $1
 [)-. ]*                   # optional common delimiters after area code, not captured
 (                         # begin capture group $2 for exchange code
  [2-9]                    # first digit cannot be a 1
  (?:[02-9][0-9]|1[02-9])) # second and third digit cannot be "11" 
 )                         # end capture group for exchange
 [-. ]?                    # common delimiters between exchange and SN, not captured
 ([0-9]{4})                # subscriber number, captured in $3
 (?:                       # start non-capturing group for optional extension 
 \s*(?:x|ext|ext.)\s*      # common prefixes before extension numbers
 (\d+)                     # optional extension, captured in $4
 ){0,1}                    # end non-capturing group
 $                         # end of string, or EOL in multi-line mode

这会处理呼叫代码(可选)、半验证区号(可选)和交换代码、分机号码(可选),并将电话号码的每个部分捕获在一个单独的变量中,以便于提取和操作。

在 .NET 中使用此表达式,您需要包含 IgnorePatternWhitespace 和 MultiLine 标志,以便忽略逗号,^$ 字符可在字符串中的任意行查找电话号码。

【讨论】:

    【解决方案2】:
    Match matchResults = null;
    try {
        Regex regexObj = new Regex(@"\(?\b[0-9]{3}\)?[-. ]?[0-9]{3}[-. ]?[0-9]{4}\b");
        matchResults = regexObj.Match(subjectString);
        if (matchResults.Success) {
            // matched text: matchResults.Value
            // match start: matchResults.Index
            // match length: matchResults.Length
            // backreference n text: matchResults.Groups[n].Value
            // backreference n start: matchResults.Groups[n].Index
            // backreference n length: matchResults.Groups[n].Length
        } else {
            // Match attempt failed
        } 
    } catch (ArgumentException ex) {
        // Syntax error in the regular expression
    }
    

    这个sn-p是从RegexBuddy那里得到的,Regex的好帮手。

    【讨论】:

    • 谢谢伙计!但是,我正在使用以下正则表达式: ((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}
    • 这很好,但如果没有提供区号,则不起作用,如果输入包含字母“x”后面的分机号码,也不起作用(因为\b不会看到单词边界)。接受区号和交换中的任何数字也太混杂了——允许的数字在一些地方稍微严格一些(当然,这很挑剔,但就像对 IP 地址使用正则表达式一样,理想情况下,捕获的数字范围应该限制在仅允许的值)。
    • @richardtallent:谢谢;但主要目的是由labilbe的解决方案服务的。关于您粘贴的解决方案,它也很棒!
    猜你喜欢
    • 2012-06-06
    • 2014-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-25
    • 2011-06-16
    • 1970-01-01
    相关资源
    最近更新 更多