简体   繁体   English

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

[英]Replication of digits in a string using regex sub (Python)

I have a s="gh3wef2geh4ht" . 我有一个s="gh3wef2geh4ht" How can I receive s="gh333wef22geh4444ht" by using sub. 如何通过使用sub接收s="gh333wef22geh4444ht" I have tried this regexp . 我试过这个正则表达式 what I am doing wrong? 我做错了什么?

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

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'

You cannot use a regular expression pattern in the replacement pattern. 您无法在替换模式中使用正则表达式模式。 The {...} does not copy the text captured in Group 1 n times. {...}不会复制第1组中捕获的文本n次。 You need to use a lambda expression or a callback method in the re.sub to achieve what you want: 你需要在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 . 这里, m被赋予当前匹配对象, m.group()是匹配的文本值,而int(m.group())是转换为int的数字。 Thus, when 3 is matched, the lambda expression does just "3" * 3 and returns as the replacement. 因此,当匹配3时,lambda表达式只做"3" * 3并作为替换返回。

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

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