Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex python正则表达式不使用\b计算_Regex_Python 3.x - Fatal编程技术网

Regex python正则表达式不使用\b计算

Regex python正则表达式不使用\b计算,regex,python-3.x,Regex,Python 3.x,我试图对带破折号的字符串进行简单的边界正则表达式替换,但它们不会替换,为什么,破折号在正则表达式中有什么意义吗 下面是我的实际尝试,其中f是我要替换字符串完全匹配的变量: >>> import re >>> f = "user-services-http-two user-services-http" >>> re.sub(r'\buser-services-http\b', '32370', f) '32370-two 32370' 我如

我试图对带破折号的字符串进行简单的边界正则表达式替换,但它们不会替换,为什么,破折号在正则表达式中有什么意义吗

下面是我的实际尝试,其中f是我要替换字符串完全匹配的变量:

>>> import re
>>> f = "user-services-http-two user-services-http"
>>> re.sub(r'\buser-services-http\b', '32370', f)
'32370-two 32370'
我如何获得以下各项的预期输出:

'user-services-http-two 32370'
我的要求是我必须使用re,因为我稍后在代码中使用的是python中的另一个模块(massedit)

import re

f = "user-services-http-two user-services-http"
re.sub('\\b(user-services-http$)', '32370', f)
根据您上面的额外评论进行编辑-但与PKumar相同-它适用于您的所有案例

您可以这样做:

re.sub(r'\buser-services-http$', '32370', f)