如何使用Python的re.sub来匹配和替换组零次或多次?

时间:2016-12-23 14:24:54

标签: python regex

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)

我想更换主机名&#39; aaa.bbb.ccc.com&#39;到&#39; www.example.com&#39;,但正则表达式不起作用,因为url中的路径可能不存在。有没有办法匹配一个组0次或更多次?或者如何处理这个问题?

2 个答案:

答案 0 :(得分:0)

此方法可以帮助您

text_filtered = re.sub(r'<p ([^>]*)>', '<p>', text_filtered) # remove the other field from p tag

答案 1 :(得分:0)

您的第二组可以捕获除/以外的所有内容:

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)

输出:

auth_redirect_url = www.example.com/auth-web
auth_redirect_url = www.example.com