简体   繁体   English

用python中的另一个字符串替换字符串的某些出现

[英]Substitution of certain occurences of a string with another string in python

Sorry if someone already posted the same question, but I was unable to find it. 很抱歉,如果有人已发布相同的问题,但我无法找到它。

I am trying to replace certain occurrences of a string pattern with something else. 我试图用其他东西替换某些字符串模式的出现。 The problem I do not want to replace all occurrences, just all apart from one. 问题我不想替换所有事件,只是除了一个。

For example. 例如。 Imagine I have the string: '(M:2,Seq0:2):10,Seq1:20,(Seq2:40,Seq3:40)' The pattern I want to find is: '\\w+\\d+:\\d' (which refer to Seq[number] ) 想象一下,我有字符串: '(M:2,Seq0:2):10,Seq1:20,(Seq2:40,Seq3:40)'我想找到的模式是: '\\w+\\d+:\\d' (参考Seq[number]

Imagine I want to change all numbers after 'Seq[number]:' but not the one following for example, 'Seq1:' 想象一下,我想在'Seq[number]:'之后更改所有数字'Seq[number]:'但不是后面的那个,例如'Seq1:'

Imagine that to all these numbers after Seq[number]: I wanna sum the value of 10 想象一下,在Seq[number]:之后的所有这些数字Seq[number]:我想要将值10加起来

in The end I would like to have the string: 在最后我想有字符串:

'(M:2,Seq0:12):10,Seq1:20,(Seq2:50,Seq3:50)'

Is there a way of doing this in a loop? 有没有办法在循环中这样做? I tried to use re.findall, but it returns all occurences in a text. 我尝试使用re.findall,但它返回文本中的所有出现。 How could I incorporate this in a loop? 我怎么能把它合并到一个循环中?

Thanks! 谢谢!

You can do this using re.sub with a function as the replacement, for example: 您可以使用带有函数的re.sub作为替换,例如:

>>> import re
>>> s = '(M:2,Seq0:2):10,Seq1:20,(Seq2:40,Seq3:40)'
>>> def repl(match):
...     return match.group(1) + str(int(match.group(2)) + 10)
...
>>> re.sub(r'(\w+(?!1:)\d+:)(\d+)', repl, s)
'(M:2,Seq0:12):10,Seq1:20,(Seq2:50,Seq3:50)'

The restriction to not match Seq1: is handled by the negative lookahead (?!1:) , the capturing groups are used just to separate the portion of the string that you want to modify from the rest of it. Seq1:不匹配的限制由否定前瞻(?!1:) ,捕获组仅用于将要修改的字符串部分与其余部分分开。 The replacement function then returns group 1 unchanged plus the value from group 2 plus 10. 替换函数然后返回组1不变加上组2加上10的值。

As suggested by Cilyan in comments, you could also add the restriction to not replace for Seq1: in the replacement function, which simplifies the regex. 正如Cilyan在评论中所建议的那样,您还可以在替换函数中添加限制以不替换Seq1:这简化了正则表达式。 Here is how this would look: 这是这样的:

def repl(match):
    if match.group(1) == 'Seq1:':
        return match.group(0)
    return match.group(1) + str(int(match.group(2)) + 10)

result = re.sub(r'(\w+\d+:)(\d+)', repl, s)

edit: To address the questions in your comment, here is how you could write this to modify the number that you add and which prefix (like Seq1:) should be ignored: 编辑:为了解决你的评论中的问题,这里是你如何写这个来修改你添加的数字和应该忽略哪个前缀(如Seq1 :):

def make_repl(n, ignore):
    def repl(match):
        if match.group(1) == ignore:
            return match.group(0)
        return match.group(1) + str(int(match.group(2)) + n)
    return repl

result = re.sub(r'(\w+\d+:)(\d+)', make_repl(10, 'Seq1:'), s)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM