相关疑难解决方法(0)

从re.sub.调用函数

这是一个简单的例子:

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 而不是35.

为什么?我该如何解决?

python regex windows function

15
推荐指数
2
解决办法
7188
查看次数

正则表达式为匹配的字符串添加字符

我有一个很长的字符串,这是一个段落,但是在句号之后没有空格.例如:

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)

python regex nlp

8
推荐指数
1
解决办法
4850
查看次数

Python lambda与正则表达式

当使用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)

python regex lambda python-2.7

6
推荐指数
1
解决办法
7496
查看次数

标签 统计

python ×3

regex ×3

function ×1

lambda ×1

nlp ×1

python-2.7 ×1

windows ×1