python - 匹配电话号码,正则表达式

标签 python regex

我有这种格式的电话号码:

 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/

相关文章:

regex - awk sub 用一个捕获组替换成

javascript - jQuery - 查找并替换子字符串

python - Matplotlib/python 可点击点

python - 如何在 Python PIL 中为绘制文本设置适当的线宽?

python - 手动向 Seaborn Legend 添加图例条目

ruby - 消极的向后看,中间有东西

python - 正则表达式帮助 - 分隔符

javascript - 防止特殊字符 - jQuery 表单验证

python - Pandas ValueError 返回的形状与数据框形状不匹配?

带有空格或特殊字符的 Python 类型定义