使用regex sub(Python)复制字符串中的数字

时间:2021-05-07 19:29:55

I have a s="gh3wef2geh4ht". How can I receive s="gh333wef22geh4444ht" by using sub. I have tried this regexp. what I am doing wrong?

我有一个s =“gh3wef2geh4ht”。如何使用sub接收s =“gh333wef22geh4444ht”。我试过这个正则表达式。我做错了什么?

s=re.sub(r"(\d)",r"\1{\1}",s)

2 个解决方案

#1


2  

You cannot use a regular expression pattern in the replacement pattern. The {...} does not copy the text captured in Group 1 n times. You need to use a lambda expression or a callback method in the re.sub to achieve what you want:

您无法在替换模式中使用正则表达式模式。 {...}不会复制第1组中捕获的文本n次。你需要在re.sub中使用lambda表达式或回调方法来实现你想要的:

import re
s = 'gh3wef2geh4ht'
s=re.sub(r"\d", lambda m: m.group() * int(m.group()), s)
print(s)

See the Python demo

请参阅Python演示

Note you do not need any capturing groups here, as the whole match is already available in Group 0.

请注意,此处不需要任何捕获组,因为整个匹配已在组0中可用。

Here, m is assigned with the curren match object, m.group() is the text value of the match and int(m.group()) is the digit cast to an int. Thus, when 3 is matched, the lambda expression does just "3" * 3 and returns as the replacement.

这里,m被赋予当前匹配对象,m.group()是匹配的文本值,而int(m.group())是转换为int的数字。因此,当匹配3时,lambda表达式只做“3”* 3并作为替换返回。

#2


4  

You can use a lambda function to capture the matched digits and repeat it:

您可以使用lambda函数捕获匹配的数字并重复它:

s="gh3wef2geh4ht"
​
re.sub(r'(\d)', lambda m: m.group(1) * int(m.group(1)), s)
# 'gh333wef22geh4444ht'

#1


2  

You cannot use a regular expression pattern in the replacement pattern. The {...} does not copy the text captured in Group 1 n times. You need to use a lambda expression or a callback method in the re.sub to achieve what you want:

您无法在替换模式中使用正则表达式模式。 {...}不会复制第1组中捕获的文本n次。你需要在re.sub中使用lambda表达式或回调方法来实现你想要的:

import re
s = 'gh3wef2geh4ht'
s=re.sub(r"\d", lambda m: m.group() * int(m.group()), s)
print(s)

See the Python demo

请参阅Python演示

Note you do not need any capturing groups here, as the whole match is already available in Group 0.

请注意,此处不需要任何捕获组,因为整个匹配已在组0中可用。

Here, m is assigned with the curren match object, m.group() is the text value of the match and int(m.group()) is the digit cast to an int. Thus, when 3 is matched, the lambda expression does just "3" * 3 and returns as the replacement.

这里,m被赋予当前匹配对象,m.group()是匹配的文本值,而int(m.group())是转换为int的数字。因此,当匹配3时,lambda表达式只做“3”* 3并作为替换返回。

#2


4  

You can use a lambda function to capture the matched digits and repeat it:

您可以使用lambda函数捕获匹配的数字并重复它:

s="gh3wef2geh4ht"
​
re.sub(r'(\d)', lambda m: m.group(1) * int(m.group(1)), s)
# 'gh333wef22geh4444ht'