这是一个简单的例子:
import re
math='<m>3+5</m>'
print re.sub(r'<(.)>(\d+?)\+(\d+?)</\1>', int(r'\2') + int(r'\3'), math)
Run Code Online (Sandbox Code Playgroud)
它给了我这个错误:
ValueError: invalid literal for int() with base 10: '\\2'
Run Code Online (Sandbox Code Playgroud)
它发送\\2 而不是3和5.
为什么?我该如何解决?
我有一个很长的字符串,这是一个段落,但是在句号之后没有空格.例如:
para = "I saw this film about 20 years ago and remember it as being particularly nasty. I believe it is based on a true incident: a young man breaks into a nurses\' home and rapes, tortures and kills various women.It is in black and white but saves the colour for one shocking shot.At the end the film seems to be trying to make some political statement but it just comes across as confused and obscene.Avoid."
Run Code Online (Sandbox Code Playgroud)
我试图使用re.sub来解决这个问题,但输出不是我的预期.
这就是我做的:
re.sub("(?<=\.).", …Run Code Online (Sandbox Code Playgroud) 当使用re的re.sub()部分为python时,如果我没有弄错,可以使用一个函数作为sub.据我所知,它将匹配传递给传递的任何函数,例如:
r = re.compile(r'([A-Za-z]')
r.sub(function,string)
Run Code Online (Sandbox Code Playgroud)
除了使用调用方法的lambda之外,还有更聪明的方法让它传递给第二个arg吗?
r.sub(lambda x: function(x,arg),string)
Run Code Online (Sandbox Code Playgroud)