Python正则表达式sub,具有1个以下参数
我将如何改变Python正则表达式sub,具有1个以下参数,python,regex,Python,Regex,我将如何改变 "Take dog1 and dog5 to the dog kennel" 到 ??所以我只想在“dog”后面有数字的时候用“animal”来代替“dog”。 欢迎提供任何帮助。您可能可以执行以下操作: import re old_sting = 'Take dog1 and dog5 to the dog kennel' dog_finder = re.compile(r'dog(\d+)') new_string = re.sub(dog_finder, lambda
"Take dog1 and dog5 to the dog kennel"
到
??所以我只想在“dog”后面有数字的时候用“animal”来代替“dog”。
欢迎提供任何帮助。您可能可以执行以下操作:
import re
old_sting = 'Take dog1 and dog5 to the dog kennel'
dog_finder = re.compile(r'dog(\d+)')
new_string = re.sub(dog_finder, lambda dog: 'animal_' + dog.group(1), old_sting)
您可能可以执行以下操作:
import re
old_sting = 'Take dog1 and dog5 to the dog kennel'
dog_finder = re.compile(r'dog(\d+)')
new_string = re.sub(dog_finder, lambda dog: 'animal_' + dog.group(1), old_sting)
您可能可以执行以下操作:
import re
old_sting = 'Take dog1 and dog5 to the dog kennel'
dog_finder = re.compile(r'dog(\d+)')
new_string = re.sub(dog_finder, lambda dog: 'animal_' + dog.group(1), old_sting)
您可能可以执行以下操作:
import re
old_sting = 'Take dog1 and dog5 to the dog kennel'
dog_finder = re.compile(r'dog(\d+)')
new_string = re.sub(dog_finder, lambda dog: 'animal_' + dog.group(1), old_sting)
大概是这样的:
>>> import re
>>> def dog_replace(matchobj):
... number = matchobj.group(0)[-1:];
... return "animal_"+ number
>>> re.sub('dog[0-9]', dog_replace, "Take dog1 and dog5 to the dog kennel")
"Take animal_1 and animal_5 to the dog kennel"
这是我在本地机器上测试过的。它本质上与lambda相同……我只是一只老熊,我认为这更具可读性。类似这样的内容:
>>> import re
>>> def dog_replace(matchobj):
... number = matchobj.group(0)[-1:];
... return "animal_"+ number
>>> re.sub('dog[0-9]', dog_replace, "Take dog1 and dog5 to the dog kennel")
"Take animal_1 and animal_5 to the dog kennel"
这是我在本地机器上测试过的。它本质上与lambda相同……我只是一只老熊,我认为这更具可读性。类似这样的内容:
>>> import re
>>> def dog_replace(matchobj):
... number = matchobj.group(0)[-1:];
... return "animal_"+ number
>>> re.sub('dog[0-9]', dog_replace, "Take dog1 and dog5 to the dog kennel")
"Take animal_1 and animal_5 to the dog kennel"
这是我在本地机器上测试过的。它本质上与lambda相同……我只是一只老熊,我认为这更具可读性。类似这样的内容:
>>> import re
>>> def dog_replace(matchobj):
... number = matchobj.group(0)[-1:];
... return "animal_"+ number
>>> re.sub('dog[0-9]', dog_replace, "Take dog1 and dog5 to the dog kennel")
"Take animal_1 and animal_5 to the dog kennel"
这是我在本地机器上测试过的。它本质上与lambda相同…我只是一只老熊,我认为这更具可读性。您可以尝试下面的
re.sub
功能
>>> import re
>>> s = "Take dog1 and dog5 to the dog kennel"
>>> m = re.sub(r'dog(\d)', r'animal_\1', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
通过前瞻,
>>> m = re.sub(r'dog(?=\d)', r'animal_', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
您可以尝试下面的
re.sub
功能
>>> import re
>>> s = "Take dog1 and dog5 to the dog kennel"
>>> m = re.sub(r'dog(\d)', r'animal_\1', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
通过前瞻,
>>> m = re.sub(r'dog(?=\d)', r'animal_', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
您可以尝试下面的
re.sub
功能
>>> import re
>>> s = "Take dog1 and dog5 to the dog kennel"
>>> m = re.sub(r'dog(\d)', r'animal_\1', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
通过前瞻,
>>> m = re.sub(r'dog(?=\d)', r'animal_', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
您可以尝试下面的
re.sub
功能
>>> import re
>>> s = "Take dog1 and dog5 to the dog kennel"
>>> m = re.sub(r'dog(\d)', r'animal_\1', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
通过前瞻,
>>> m = re.sub(r'dog(?=\d)', r'animal_', s)
>>> m
'Take animal_1 and animal_5 to the dog kennel'
你试过做些调查吗?最简单的方法是匹配数字并将其放回替换项中。另一种方法是使用前瞻,我已经阅读了regex上的python文档、tutorialspoint文档以及这些留言板。我一直在和我的翻译鬼混,运气不好。我可以用普通的python来做,但我想用正则表达式有一条捷径。你利用了它们吗?前瞻本身通常需要一段时间才能理解。我是通过快速阅读python正则表达式函数得到答案的。但我下面的也是最基本的。@Jerry我说这就是我找到解决方案的原因(回答如下)。我想你是指那个问这个问题的人(用户3319934)你有没有试着做一些研究?最简单的方法是匹配数字并将其放回替换项中。另一种方法是使用前瞻,我已经阅读了regex上的python文档、tutorialspoint文档以及这些留言板。我一直在和我的翻译鬼混,运气不好。我可以用普通的python来做,但我想用正则表达式有一条捷径。你利用了它们吗?前瞻本身通常需要一段时间才能理解。我是通过快速阅读python正则表达式函数得到答案的。但我下面的也是最基本的。@Jerry我说这就是我找到解决方案的原因(回答如下)。我想你是指那个问这个问题的人(用户3319934)你有没有试着做一些研究?最简单的方法是匹配数字并将其放回替换项中。另一种方法是使用前瞻,我已经阅读了regex上的python文档、tutorialspoint文档以及这些留言板。我一直在和我的翻译鬼混,运气不好。我可以用普通的python来做,但我想用正则表达式有一条捷径。你利用了它们吗?前瞻本身通常需要一段时间才能理解。我是通过快速阅读python正则表达式函数得到答案的。但我下面的也是最基本的。@Jerry我说这就是我找到解决方案的原因(回答如下)。我想你是指那个问这个问题的人(用户3319934)你有没有试着做一些研究?最简单的方法是匹配数字并将其放回替换项中。另一种方法是使用前瞻,我已经阅读了regex上的python文档、tutorialspoint文档以及这些留言板。我一直在和我的翻译鬼混,运气不好。我可以用普通的python来做,但我想用正则表达式有一条捷径。你利用了它们吗?前瞻本身通常需要一段时间才能理解。我是通过快速阅读python正则表达式函数得到答案的。但我下面的也是最基本的。@Jerry我说这就是我找到解决方案的原因(回答如下)。我想你是指那个问这个问题的人(用户3319934)啊!这就是我要找的。它起作用了。我找不到如何引用该参数。“\1”成功了。再次感谢。我是这里的傻瓜。啊!这就是我要找的。它起作用了。我找不到如何引用该参数。“\1”成功了。再次感谢。我是这里的傻瓜。啊!这就是我要找的。它起作用了。我找不到如何引用该参数。“\1”成功了。再次感谢。我是这里的傻瓜。啊!这就是我要找的。它起作用了。我找不到如何引用该参数。“\1”成功了。再次感谢。我是这里的傻瓜。