我有这种格式的电话号码:
some_text phone_number some_text
some_text (888) 501-7526 some_text
哪种搜索电话号码的方式更像 pythonic
(\(\d\d\d\) \d\d\d-\d\d\d\d)
(\([0-9]+\) [0-9]+-[0-9]+)
或者有更简单的表达式来做到这一点?
最佳答案
我认为您正在寻找这样的东西:
(\(\d{3}\) \d{3}-\d{4})
来自Python docs :
{m}
Specifies that exactly m copies of the previous RE should be matched; fewer matches cause the entire RE not to match. For example, a{6} will match exactly six 'a' characters, but not five.
(\(\d\d\d\)\d\d\d-\d\d\d\d)
也可以,但是,正如您在问题中所说,比较重复。您建议的其他模式 (\([0-9]+\) [0-9]+-[0-9]+)
在输入时给出误报,例如 (1 ) 2-3
.
关于python - 匹配电话号码,正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35922714/