[英]How to use Python's re.sub to match and replace group zero or more times?
import re
host = 'www.example.com'
urls = [
'auth_redirect_url = aaa.bbb.ccc.com/auth-web',
'auth_redirect_url = aaa.bbb.ccc.com'
]
for url in urls:
print re.sub(r'(^auth_redirect_url\s*=\s*)(.*)(/.*)', r'\g<1>{}\g<3>'.format(host), url)
I want to replace the host name 'aaa.bbb.ccc.com' to 'www.example.com', but the regex does not work, because the path in url may not exist. 我想将主机名'aaa.bbb.ccc.com'替换为'www.example.com',但是正则表达式不起作用,因为url中的路径可能不存在。 Is there a way to match a group 0 or more times?
有没有办法匹配一个组0次或多次? Or how to deal with this problem?
还是如何处理这个问题?
此方法可能对您有帮助
text_filtered = re.sub(r'<p ([^>]*)>', '<p>', text_filtered) # remove the other field from p tag
Your second group could capture everything except /
: 您的第二组可以捕获除
/
以外的所有内容:
import re
host = 'www.example.com'
urls = [
'auth_redirect_url = aaa.bbb.ccc.com/auth-web',
'auth_redirect_url = aaa.bbb.ccc.com'
]
for url in urls:
print re.sub(r'(^auth_redirect_url\s*=\s*)([^/]*)(.*)',
r'\g<1>{}\g<3>'.format(host), url)
Output: 输出:
auth_redirect_url = www.example.com/auth-web
auth_redirect_url = www.example.com
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.