-1

我有以下陈述的文件。

start < some 50 words > End          //need to work only on these types
start < some 50 words >
start < some 50 words > End
start < some 50 words > 
< some 50 words > End

...这种模式重复了 10000 次。我想用开头和结尾的“开始”替换行

start2 <same 50 words > End2.

我需要在两者之间保留相同的单词,只需修改开始和结束。

4

2 回答 2

1
import re

data = """start < some 50 words > End
start < some 50 words >
start < some 50 words > End
start < some 50 words >
< some 50 words > End
"""

print re.sub('start(.*)End', 'start2\g<1>End.', data)

印刷:

start2 < some 50 words > End.
start < some 50 words >
start2 < some 50 words > End.
start < some 50 words >
< some 50 words > End
于 2013-08-11T22:56:37.370 回答
1

对于这个问题,正则表达式比你需要做的工作要多——这一切都可以用普通的旧字符串方法更简单地完成:

def ReplaceStartEnd(s):
...    if s.startswith("start") and s.endswith("End"):
...       return "start2" + s[5:-3] + "End2"
...    else:
...       return s
于 2013-08-11T23:09:08.743 回答