如何匹配模式并为其添加字符

时间:2019-01-09 21:24:04

标签: python regex

我有类似的东西:

GCF_002904975:2.6672e-05):2.6672e-05.
我想在下一个冒号之前的任何GCF(任何数字)条目之后添加单词“ _S”。

换句话说,我希望我的文字变得像这样:

GCF_002904975_S:2.6672e-05):2.6672e-05.
我一直在文字中重复这样的模式。

3 个答案:

答案 0 :(得分:2)

This can be easily done with re.sub function. A working example would look like this:

import re

inp_string='(((GCF_001297375:2.6671e-05,GCF_002904975:2.6672e-05)0.924:0.060046136,(GCF_000144955:0.036474926,((GCF_001681075:0.017937143,...'

if __name__ == "__main__":
    outp_string = re.sub(r'GCF_(?P<gfc_number>\d+)\:', r'GCF_\g<gfc_number>_S:', inp_string)
    print(outp_string)

This code gives the following result, which is hopefully what you need:

(((GCF_001297375_S:2.6671e-05,GCF_002904975_S:2.6672e-05)0.924:0.060046136,(GCF_000144955_S:0.036474926,((GCF_001681075_S:0.017937143,...

For more info take a look at the docs: https://docs.python.org/3/library/re.html

答案 1 :(得分:1)

You can use regular expressions with a function substitution. The solution below depends on the numbers always being 9 digits, but could be modified to work with other cases.

test_str = '(((GCF_001297375:2.6671e-05,GCF_002904975:2.6672e-05)0.924:0.060046136,GCF_000144955:0.036474926,((GCF_001681075:0.017937143,...'
new_str = re.sub(r"GCF_\d{9}", lambda x: x.group(0) + "_S", test_str)

print(new_str)
#(((GCF_001297375_S:2.6671e-05,GCF_002904975_S:2.6672e-05)0.924:0.060046136,GCF_000144955_S:0.036474926,((GCF_001681075_S:0.017937143,...

答案 2 :(得分:0)

为什么不只是替换?缩短示例字符串,使其更易于阅读:

"(((GCF_001297375:2.6671e-05,GCF_002904975:2.6672e-05)...".replace(":","_S:")