python
例如
PRINTABLE:张三TEL;VOICE:13882200000TEL:+8618674060000
PRINTABLE:王二TEL;VOICE:13987200000
PRINTABLE:李四TEL;VOICE:13987200000TEL:+861549860000TEL:18674060000
我需要匹配上面的电话号码.有些只有一个.但是有些是连个.或者更多..
请问怎么写正则匹配呢??
我写的遇到有多个号码的就会出问题.
python
例如
PRINTABLE:张三TEL;VOICE:13882200000TEL:+8618674060000
PRINTABLE:王二TEL;VOICE:13987200000
PRINTABLE:李四TEL;VOICE:13987200000TEL:+861549860000TEL:18674060000
我需要匹配上面的电话号码.有些只有一个.但是有些是连个.或者更多..
请问怎么写正则匹配呢??
我写的遇到有多个号码的就会出问题.
收起
当前问题酬金
¥ 0 (可追加 ¥500)
支付方式
扫码支付
PRINTABLE:([\u4e00-\u9fa5]+)TEL;VOICE:([0-9]+)((TEL:[+]*[0-9]*)*)
没有按照电话号码的标准来嗷,不过也差不多
A:
简答:同一行内,获取不同数据,用re.findall
注:而不是search,我最开始也差点搞错,写成search多个的group了,发现不对,改为findall,即可
详解:
核心代码:
multipleLinePhoneStr = """PRINTABLE:张三TEL;VOICE:13882200000TEL:+8618674060000
PRINTABLE:王二TEL;VOICE:13987200000
PRINTABLE:李四TEL;VOICE:13987200000TEL:+8615498601234TEL:18674060000"""
# phoneRule = "((\+86)?\d{11})"
phoneRule = "((VOICE:)|(TEL:))((\+86)?\d{11})"
matchPhoneList = re.findall(phoneRule, multipleLinePhoneStr, re.MULTILINE)
或
inputPhoneStrList = [
"PRINTABLE:张三TEL;VOICE:13882200000TEL:+8618674060000",
"PRINTABLE:王二TEL;VOICE:13987200000",
"PRINTABLE:李四TEL;VOICE:13987200000TEL:+8615498601234TEL:18674060000",
]
for curIdx, eachInputPhoneStr in enumerate(inputPhoneStrList):
phoneRule = "((VOICE:)|(TEL:))((\+86)?\d{11})+"
matchPhoneList = re.findall(phoneRule, eachInputPhoneStr, re.DOTALL)
VSCode调试结果:
完整演示代码,详见我的帖子:
【问题解答】Python提取一个或多个电话号码
报告相同问题?