Python 查找本地电话号码的正则表达式不';行不通
我在条件面前评论了它应该对应什么样的本地电话号码Python 查找本地电话号码的正则表达式不';行不通,python,regex,Python,Regex,我在条件面前评论了它应该对应什么样的本地电话号码 enter code here if re.match(r"^(\+225)\d{8}", line): # +22588241628 count += 1 elif re.match(r"^(\+225)\s\d{8}", line): # +225 88241628 count += 1 elif re
enter code here
if re.match(r"^(\+225)\d{8}", line): # +22588241628
count += 1
elif re.match(r"^(\+225)\s\d{8}", line): # +225 88241628
count += 1
elif re.match(r"^(\+225)\s\d{2}\s\d{2}\s\d{2}\s\d{2}", line): # +225 88 24 16 28
更新了正则表达式:
\+?225[-.\s|]?(\d{8}|\d{2}[-.\s]\d{2}[-.\s]\d{2}[-.\s]\d{2})
\+?什么不起作用?不起作用的是模式,当我测试它时,我认为它起作用了。请看一个简短的例子,因为您正在使用Use re.search并删除锚^
像if re.search(r“(\+225)\d{8}”,行):
啊,是的,这是真的,谢谢。它对+221或+223有效。我希望它仅限于+225开头的数字。非常感谢,它非常有效。你是个杀手
\+? <- One or Zero +
225 <- must be exact
[-.\s|]? <- On or Zero symbols of: -.\s| ( \s is a space)
Now there is an OR statement
(A|B) <-- A or B
A = \d{8} <-- 8 digital characters after each other
B = \d{2}[-.\s]\d{2}[-.\s]\d{2}[-.\s]\d{2} <-- 2 digits
a seperator -. or \s
2 digits
a seperator -. or \s
2 digits
a seperator -. or \s
2 digits