這將爲數字工作在美國:
^ # 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和多標誌等等逗號被忽略,^
和$
字符的字符串中的任何一行查找電話號碼。
C#沒有正則表達式 – 2010-02-14 08:49:37
@John:我的意思是我需要在C#中使用正則表達式。不過謝謝你說清楚。 – effkay 2010-02-14 13:01:02