Python regex是正确的,但返回None

时间:2019-07-30 18:23:00

标签: python regex

我的Regex必须从字符串中识别联系人号码。这是正确的,我已经在线上对我的样本进行了测试,这里是https://regex101.com/r/Q2Z6fy/1的示例。但是当我在我的python代码中运行它时。它不返回

这是应该正确将字符串识别为匹配项的代码。

function BDImage(path) {
    const img = new Image();
    img.src = path; 
    return img;
}; 

const myImage = BDImage("path/img.png");

预期结果是String本身,但是正则表达式返回None。

1 个答案:

答案 0 :(得分:1)

使用re.findall()代替re.match()

使用以下代码

import re

regex = r"(?:(?:\(\+92\)|\+92) (?:42|332)|0332) ?\d+(?:([ -])\d+(?:\1\d+)*)?"

test_str = ("+92 42 111-865-865\n"
            "(+92) 42 3256 0445\n"
            "03325138889\n"
            "0332 5138889\n"
            "+92 332 5138889\n"
            "+92 3325138889\n\n"
            "48358982872144\n"
            "100220100\n"
            "36470002")

matches = re.finditer(regex, test_str, re.MULTILINE)
for matchNum, match in enumerate(matches, start=1):
    for groupNum in range(0, len(match.groups())):
        print(match.group(groupNum))