[英]how do I find a 10-digit number according to the first three numbers in the text? Python
我需要在文本中找到以某個數字系列開頭的所有 10 位數字。 有一個例子:
a_string = "一些文本 6401104219 和 6401104202 和 2201104202"
匹配 = ["240", "880", "898", "910", "920", "960", "209", "309", "409", "471", "640"]
結果是:6401104219, 6401104202
您可以使用正則表達式和str.startswith
:
import re
result = [s for s in re.findall(r"\d{10}", a_string) if any(map(s.startswith, matches))]
# ['6401104219', '6401104202']
如果你知道前綴都是 3 位數長,你可以做得更好:
matches = set(matches)
result = [s for s in re.findall(r"\d{10}", a_string) if s[:3] in matches]
如果您想排除可能的 10 位數字前綴的較長數字,則必須將正則表達式更改為r"\b(\d{10})\b"
。
你可以直接使用 re.
import re
a_string = "Some text 6401104219 and 6401104202 and 2201104202 and 640110420212"
matches = ["240", "880", "898", "910", "920", "960", "209", "309", "409", "471", "640"]
result = re.findall(r"\b(?:" + r"|".join(matches)+r")\d{7}\b", a_string)
print(result)
# ['6401104219', '6401104202']
你可以正則表達式。
代碼:
import re
a_string = "Some text 6401104219 and 6401104202 and 2201104202"
matches = ["240", "880", "898", "910", "920",
"960", "209", "309", "409", "471", "640"]
match = re.findall(r'\d{10}', a_string) # finding all the 10 digit numbers
# filtering out the numbers which starts from the given elements in matches
ans = [i for i in match if any(map(i.startswith, matches))]
# OR
# ans = [i for i in match if i[:3] in matches] # if lenght is 3 only then simply check its existence in list
print(ans)
# ['6401104219', '6401104202']
a_string = "Some text 6401104219 and 6401104202 and 2201104202 and 640110420212"
matches = ["240", "880", "898", "910", "920", "960", "209", "309", "409", "471", "640"]
a_string_list=a_string.split(' ')
for i in a_string_list:
for j in matches:
if i.startswith(j) and len(i)==10:
print(i)
break
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.