簡體   English   中英

正則表達式匹配不由 3 個相同數字組成的重復 3 位數字組

[英]Regex to match repeating 3 digit groups that arent made up of 3 identical digits

我正在嘗試匹配出現在英國電話號碼中的重復 3 位數組。 當具有此模式r'(\d)\1{2}'的組中的 3 個數字相同時,我已經可以得到匹配

例如,當輸入為“07119777777”時,我得到兩個匹配項:

<re.Match object; span=(5, 8), match='777'>
<re.Match object; span=(8, 11), match='777'>

但是,當輸入類似於“07123123123”時,由於 3 位組內的數字不同,因此我沒有匹配項。 是否有正則表達式模式將這些標識為匹配項?

請您嘗試以下方法:

str = "07590759759"
m = re.search(r'(\d{3}).*?(\1).*?(\1)', str)
print(m.groups())

Output:

('759', '759', '759')

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM