python正则表达式匹配和替换

时间:2010-08-22 21:47:26

标签: python regex

我需要找到,处理和删除(逐个)任何匹配相当长的正则表达式的子串:

# p is a compiled regex
# s is a string  
while 1:
    m = p.match(s)
    if m is None:
        break
    process(m.group(0)) #do something with the matched pattern
    s = re.sub(m.group(0), '', s) #remove it from string s

上述代码有两个原因:

  1. 如果m.group(0)恰好包含任何正则表达式特殊字符(如*,+等),则不起作用。

  2. 感觉我正在复制这项工作:首先我在字符串中搜索正则表达式,然后我必须再次寻找它以删除它。

  3. 这样做的好方法是什么?

1 个答案:

答案 0 :(得分:20)

re.sub函数可以将函数作为参数,以便您可以根据需要组合替换和处理步骤:

# p is a compiled regex
# s is a string  
def process_match(m):
    # Process the match here.
    return ''

s = p.sub(process_match, s)
相关问题