正则表达式返回两个数字?

2024-07-04 06:07:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写符合以下参数的正则表达式:

print('twonum:',
    '3,4' in tn,
    '3.0, 4.5' in tn,
    '-3.14159265 1110' in tn,
    '3.4.5, 1' not in tn,
    '1   2' not in tn,
    '3 - 4' not in tn)

for match in regex.twonum.finditer(text):
    if match.group(0) == '3,4':
        print('  match1:', '3' in match.groups(), '4' in match.groups())
    if match.group(0) == '-3.14159265 1110':
        print('  match2:', '-3.14159265' in match.groups(), '1110' in match.groups())

但是我只能使它与pythex中的'3, 4'匹配。对于前三个,它仍然返回False。对于最后三个,它确实返回True,这是应该的match1和{}仍然是个谜

这是到目前为止我的正则表达式:

pattern2 = '\'[\d](,)?[\d]\''
twonum = re.compile(pattern2)

我应该怎么做才能使它适合给定的参数


Tags: infor参数ifmatchgroupnotregex

热门问题