重复

时间:2015-07-06 16:19:14

标签: python regex

我有一个行列表。我正在编写一个典型的文本修改函数,它贯穿列表中的每一行,并在检测到模式时修改它。

我后来在写这种类型的函数时意识到模式可能会在行中重复多次。

例如,这是我写的函数之一:

def change_eq(string):
    #inputs a string and outputs the modified string
    #replaces (X####=#) to (X####==#)

    #set pattern
    pat_eq=r"""(.*)              #stuff before
            ([\(\|][A-Z]+[0-9]*) #either ( or | followed by the variable name 
            (=)                  #single equal sign we want to change 
            ([0-9]*[\)\|])       #numeric value of the variable followed by ) or |
            (.*)"""              #stuff after

    p= re.compile(pat_eq, re.X)
    p1=p.match(string)

    if bool(p1)==1: 
        # if pattern in pat_eq is detected, replace that portion of the string with a modified version
        original=p1.group(0)
        fixed=p1.group(1)+p1.group(2)+"=="+p1.group(4)+p1.group(5)
        string_c=string.replace(original,fixed)
        return string_c
    else: 
        # returns the original string
        return string

但是对于输入字符串,例如

'IF (X2727!=78|FLAG781=0) THEN PURPILN2=(X2727!=78|FLAG781=0)*X2727' 

,group()仅适用于字符串中检测到的最后一个模式,因此将其更改为

'IF (X2727!=78|FLAG781=0) THEN PURPILN2=(X2727!=78|FLAG781==0)*X2727' 

,忽略检测到的第一个案例。我明白这是我使用group属性的函数的产物。

我该如何解决这个问题?我知道有{m,n},但它是否适用于匹配?

提前谢谢你。

1 个答案:

答案 0 :(得分:1)

不同的语言处理"全球"以不同的方式匹配。您将要使用Python re.finditer (link)并使用for循环迭代生成的匹配对象。

您的部分代码示例:

p = re.compile(pat_eq, re.X)
string_c = string
for match_obj in p.finditer(string):
    original = match_obj.group(0)
    fixed = p1.group(1) + p1.group(2) + '==' + p1.group(4) + p1.group(5)
    string_c = string_c.replace(original, fixed)

return string_c