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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 正则表达式不能用边界条件代替精确匹配_Regex_Python 3.x - Fatal编程技术网

Regex 正则表达式不能用边界条件代替精确匹配

Regex 正则表达式不能用边界条件代替精确匹配,regex,python-3.x,Regex,Python 3.x,我试图创建一个regex表达式,就像bash中的sed命令一样。替换与“用户服务http”完全匹配的内容,但这也可以作为“用户服务http 2”存在于其他地方,并得到替换 >>> f = "'value: http://ec2-35-171-57-57.compute-1.amazonaws.com:32370ssoeproxy/logout value: http://ec2-35-171-57-57.compute-1.amazonaws.com:user-servi

我试图创建一个regex表达式,就像bash中的sed命令一样。替换与“用户服务http”完全匹配的内容,但这也可以作为“用户服务http 2”存在于其他地方,并得到替换

>>> f = "'value: http://ec2-35-171-57-57.compute-1.amazonaws.com:32370ssoeproxy/logout    value: http://ec2-35-171-57-57.compute-1.amazonaws.com:user-services-http-two/ssoeproxy/logout  value: user-services-http #458930'"
>>> re.sub(r'\buser-services-http$', '11111', f)'value: http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:user-services-http/ssoeproxy/logout    value: http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:user-services-http-two/ssoeproxy/logout'
>>> re.sub(r'\buser-services-http\b', '11111', f)
'value: http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:11111/ssoeproxy/logout    value: http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:11111-two/ssoeproxy/logout'

你可以用这样的消极展望:

import re

f = "value: http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:user-services-http/ssoeproxy/logout    value: http://ec2-xxx-xx-xxx-xxx.compute-1.amazonaws.com:user-services-http-two/ssoeproxy/logout"
print(re.sub(r'\buser-services-http\b(?!-[^/]+)/', '11111', f)
输出:

value: http://ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com:11111/ssoeproxy/logout
value: http://ec2-xxx-xx-xxx-xxx.compute-1.amazonaws.com:user-services-http-two/ssoeproxy/logout
确保后面没有紧跟着
-两个
或其他单词。这是通过使用负字符类
[^/]+
来完成的。尝试这种方法的好地方是:


只需在末尾添加一个
/
,即搜索
用户服务http/
,就行了吗?这真的很接近了,但这将取代搜索字符串后面的任何/,就像url一样。通讯:11111 SSOEPROXY/logout@Bohemian我更新了fB的值,但是如果后面有任何字符串呢?这只解决了-2的问题,那么-3或“/”或6789呢?啊,我还以为是-2呢,我已经更新了我的答案