[英]Regex multiple orders of named groups
I have a set of patterns that occur in multiple orders.我有一组以多个顺序出现的模式。 I'd like to refer to each pattern with a name in order to sort them and extract their information.
我想用一个名称来引用每个模式,以便对它们进行排序并提取它们的信息。 The below code doesn't work because a named group may only be defined once and putting a group in more than one operand of the |
下面的代码不起作用,因为一个命名组只能定义一次,并且将一个组放在多个 | 的操作数中。 operator is interpreted as a redefinition.
运算符被解释为重新定义。
a = r'(?P<A>AAA)'
b = r'(?P<B>BBB)'
c = r'(?P<C>CCC)'
d = r'(?P<D>DDD)'
x = r'(?P<X>XXX)'
cases = '|'.join([fr'{a}{b}{c}',
fr'{b}{c}{x}',
fr'{b}{a}{x}',
fr'{x}{d}{a}',
...])
pattern = fr'({cases})'
result = [(x.group('A'),
x.group('B'),
x.group('C'),
x.group('D'),
x.group('x'))
for x in re.finditer(pattern, long_string)]
Is there a way to put a group with the same name in different parts of the |有没有办法把同名的组放在不同的部分 | operator?
操作员?
Once you have defined your named group, you have to use named back references.一旦你定义了你的命名组,你必须使用命名的反向引用。
For exemple, to refer to the A group:以A组为例:
(?P=A)
See the documentation: https://www.regular-expressions.info/named.html?wlr=1请参阅文档: https://www.regular-expressions.info/named.html?wlr=1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.