2 回答

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)
這打?。?/p>
['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
添加回答
舉報