Python正则表达式搜索并替换所有发生的事件

时间:2017-08-03 04:16:36

标签: python regex django

我希望你能帮助我。我在python中编写代码来替换字符串上的所有匹配项,包括:@ [username](user:id)

我尝试使用以下代码,但只有在我的字符串是这样的情况下才有效:

mystring = '@[otheruser](id: 100)' and match and replace it's ok. But if I pass a string like this:

mystring = 'He is @[otheruser](id: 100) and he is @[newuser](id: 20)' doesn't work, nothing gets replaced.

代码:

import re

mystring = 'He is @[otheruser](id: 100) and he is @[newuser](id: 20)'

regex = re.compile(r'^@\[([a-zA-Z0-9]+)\]\((id: ([0-9]+))\)', re.S)
iter = re.finditer(regex, mystring)

    for result in iter:
        match = result.group()
        g1 = result.group(1)
        g2 = result.group(2)
        g3 = result.group(3)

        print(match) # full match
        print(g1) # otheruser
        print(g2) # id: number_id
        print(g3)  # number_id

        parsed_string = re.sub(p, '<a href="view/'+g3+'">@'+g1+'</a>' , mystring)

输出应该是:

He is <a href="view/100">@otheruser</a> and he is <a href="view/20">@newuser</a> doesn't work, nothing gets replaced.

2 个答案:

答案 0 :(得分:2)

正如sub方法的文档所说:

  

反序列,例如\ 6,将替换为模式中第6组匹配的子字符串。

所以,改为:

import re

mystring = 'He is @[otheruser](id: 100) and he is @[newuser](id: 20)'
my_re = r'@\[([a-zA-Z0-9]+)\]\((id: ([0-9]+))\)'  # note the missing ^ at start

regex = re.compile(my_re, re.S)
iter = re.finditer(regex, mystring)

for result in iter:
    match = result.group()
    g1 = result.group(1)
    g2 = result.group(2)
    g3 = result.group(3)

    print(match) # full match
    print(g1) # otheruser
    print(g2) # id: number_id
    print(g3)  # number_id

    # we make use of \1 to match the first matched group (which is the number_id 
    # and \3 which is the username
    parsed_string = re.sub(my_re, 
                           r'<a href="view/\3">@\1</a>', 
                           mystring)
    print(parsed_string)
    # prints
    # He is <a href="view/100">@otheruser</a> and he is <a href="view/20">@newuser</a>

答案 1 :(得分:0)

我不知道你的代码是做什么的,但是用py替换python中的字符串就像这样简单:

import re

mystring = 'He is @[otheruser](id: 100) and he is @[newuser](id: 20)'
regex = re.compile(r'@\[(\w+)\]\(id\:\s(\d+)\)', re.I)
r_p = r'<a href="view/\2">@\1</a>'
print(regex.sub(p, mystring))

请注意我删除了^(插入符号),因为它表示字符串的开头,而不是你的情况。 https://regex101.com/r/fUztdt/1