简体   繁体   English

如何在 Python 中每个正则表达式匹配组之前和之后添加字符串?

[英]How to add string before and after each regex matched groups in Python?

I have a string and a regex with several groups.我有一个字符串和一个包含多个组的正则表达式。 I want to add some string before and after each matched group.我想在每个匹配组之前和之后添加一些字符串。 How can I do it?我该怎么做? Below is the demo code only used to help describe my question.以下是仅用于帮助描述我的问题的演示代码。

data = "aa11bb123cc3333dd12eeeff3"
regex = re.compile(r"([a-z])\1(\d)(\d)(?=[a-z])")

The expected result is (using < and > to surround the matched groups):预期结果是(使用<>包围匹配的组):

<a>a<1><1>bb123cc3333<d>d<1><2>eeeff3

Please also consider regex group like (\\d)* and I don't know how many groups beforehand.还请考虑像(\\d)*这样的正则表达式组,我事先不知道有多少组。

You can use your current regex ([az])\\1(\\d)(\\d)(?=[az]) and replace it with this,您可以使用当前的正则表达式([az])\\1(\\d)(\\d)(?=[az])并将其替换为,

<\1>\1<\2><\3>

Regex Demo正则表达式演示

You can modify your code to use re.sub and get your expected output.您可以修改代码以使用re.sub并获得预期的输出。

import re

data = "aa11bb123cc3333dd12eeeff3"
regex = re.compile(r"([a-z])\1(\d)(\d)(?=[a-z])")
print(re.sub(regex, r'<\1>\1<\2><\3>', data))

Prints like you expected,打印出如您所愿,

<a>a<1><1>bb123cc3333<d>d<1><2>eeeff3

Also, as you updated your post, just clarifying that this regex (\\d)* only has one group only and it is not that there is * outside the group and it will make number of groups multiple.此外,当你更新你的帖子时,只是澄清这个正则表达式(\\d)*只有一个组,而不是组外有*并且它会使组数成倍数。 (\\d)* will behave same like \\d* except the captured digit in the group will be just one and exactly the last one that matched. (\\d)*行为与\\d*相同,除了组中捕获的数字将只是一个并且恰好是最后一个匹配的数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM