我正在尝试过滤掉特定文本中潜在的公民服务号码(荷兰语 BSN),这些文本也充满了荷兰电话号码。电话号码以 +31 国家/地区代码开头,而 BSN 号码则不然。有人可以帮我想出正则表达式来匹配任何不以 开头的 9 位数字吗+<country-code-like-prefix><space>?例如,在句子中:号码是+31 713176319,另一个号码是650068168。我想提取650068168,但不提取713176319。这可能可以通过负向预测来解决,但我无法找到正确的解决方案。
2 回答
![?](http://img1.sycdn.imooc.com/545864000001644402200220-100-100.jpg)
慕容708150
TA贡献1831条经验 获得超4个赞
我建议re.findall在这里使用:
inp = "The number is +31 713176319 and 650068168 is another one."
matches = re.findall(r'(?:^|(?<!\S)(?!\+\d+)\S+ )(\d{9})\b', inp)
print(matches)
这打印:
['650068168']
这里的正则表达式策略是匹配 9 位独立数字,当它出现在字符串的最开头时,或者它前面有一些不是国家/地区代码前缀的“单词”(此处松散定义的单词)\S+。
这是所使用的正则表达式的解释:
(?:
^ from the start of the string
| OR
(?<!\S) assert that what precedes is whitespace or start of the string
(?!\+\d+) assert that what follows is NOT a country code prefix
\S+ match the non prefix "word", followed by a space
)
(\d{9}) match and capture the 9 digit number
\b word boundary
添加回答
举报
0/150
提交
取消