2010-02-14 112 views
2

我有一个包含地址和电话号码的字符串(US格式;(xxx)xxx-xxxx)。例如,.NET正则表达式 - 查找,替换

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

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

有人可以分享快速代码段吗?

+0

C#没有正则表达式 – 2010-02-14 08:49:37

+0

@John:我的意思是我需要在C#中使用正则表达式。不过谢谢你说清楚。 – effkay 2010-02-14 13:01:02

回答

1
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 
} 

我从RegexBuddy那里得到了这个片段,这是RegEx的一个非常好的帮手。

+0

谢谢队友!然而,我正在使用以下正则表达式: ((\\(\ d {3} \\)?)|(\ d {3} - ))?\ d {3} - \ d {4} – effkay 2010-02-14 08:28:30

+1

这很好,但如果未提供区号,则不起作用,如果输入包含字母“x”后面的分机号码(因为\ b不会看到字边界)也不起作用。接受地区代码和交换中的任何数字也太混杂了 - 允许的数字在少数地方稍微受到一些限制(当然,这很挑剔,但是对于IP地址使用正则表达式,理想情况下捕获的数字范围应该限制在只有允许值)。 – richardtallent 2010-02-14 09:10:19

+0

@richardtallent:thanks;但主要目的是由labilbe的解决方案服务的。关于您粘贴的解决方案,它也非常棒! – effkay 2010-02-14 12:58:27

2

这将为数字工作在美国:

^      # 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和多标志等等逗号被忽略,^$字符的字符串中的任何一行查找电话号码。