Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中获取正则表达式中的组_Python_Regex - Fatal编程技术网

如何在python中获取正则表达式中的组

如何在python中获取正则表达式中的组,python,regex,Python,Regex,我想打印表达式中的第一个、第二个和第三个匹配组。详情如下 Regex Pattern = "(\d+)" Expression = "1123-xxx-abcd-45-tsvt-35-pwrst-99-xql" 我用的是Pythex, 它可以完美地找到并显示所有捕获的组 但它在python代码中不起作用。我下面提供的python代码,我找不到问题 import re class Test3: def printAllGroups(self): regexPatte

我想打印表达式中的第一个、第二个和第三个匹配组。详情如下

Regex Pattern = "(\d+)"
Expression = "1123-xxx-abcd-45-tsvt-35-pwrst-99-xql"
我用的是Pythex, 它可以完美地找到并显示所有捕获的组

但它在python代码中不起作用。我下面提供的python代码,我找不到问题

import re


class Test3:

    def printAllGroups(self):
        regexPattern = r"(\d+)"
        text = "1123-xxx-abcd-45-tsvt-35-pwrst-99-xql"
        matcher = re.compile(regexPattern, flags=re.IGNORECASE)
        matchValue = matcher.match(text);
        if matchValue:
            print("First group : ", matchValue.group(1))
            print("Second group : ", matchValue.group(2))
            print("Third group : ", matchValue.group(2))


if __name__ == '__main__':
    test3 = Test3()
    test3.printAllGroups()
请帮我解决这个问题,我是Python新手。

代码

import re

regexPattern = r"(\d+)"
expression = "1123-xxx-abcd-45-tsvt-35-pwrst-99-xql"
print(re.findall(regexPattern,expression))
['1123', '45', '35', '99']
输出

import re

regexPattern = r"(\d+)"
expression = "1123-xxx-abcd-45-tsvt-35-pwrst-99-xql"
print(re.findall(regexPattern,expression))
['1123', '45', '35', '99']
在当前代码中,您将出现以下错误:

    print("Second group : ", matchValue.group(2))
IndexError: no such group
因为正则表达式中只有一个组

通过以下方式更改代码,并在 ,您将有一个单独的匹配项(整行)和四个组,您可以单独访问这些组来提取数字

区分匹配(当您的正则表达式匹配/验证输入字符串时)和存储在正则表达式组中的值之间的差异很重要,每个值由括号
()

正则表达式中第一次出现的
()
可通过正则表达式(或替换字符串)或正则表达式组(1)外部的反向引用
\1
访问,正则表达式中第二次出现的
()
可通过正则表达式(或替换字符串)或
组(2)中的反向引用
\2
访问
在正则表达式之外

import re

class Test3:

    def printAllGroups(self):
        regexPattern = r"^(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+(\d+)[^\d]+$"
        text = "1123-xxx-abcd-45-tsvt-35-pwrst-99-xql"
        matcher = re.compile(regexPattern, flags=re.IGNORECASE)
        matchValue = matcher.match(text);
        if matchValue:
            print("First group : ", matchValue.group(1))
            print("Second group : ", matchValue.group(2))
            print("Third group : ", matchValue.group(3))
            print("Third group : ", matchValue.group(4))

if __name__ == '__main__':
    test3 = Test3()
    test3.printAllGroups()
输出:

python test3.py
('First group : ', '1123')
('Second group : ', '45')
('Third group : ', '35')
('Third group : ', '99')

你的预期产出是多少?仅在字符串开头检查匹配项。如果要捕获所有数字,必须使用findall而不是match。回答得很好。