1
假设我在名为“main”的列表中有一串字符串。我如何通过迭代“主,如果我找到一个匹配的话,我删除匹配部分的‘主’,然后添加匹配的文本到一个所谓的新名单‘新’?将匹配追加到新列表并从原始列表中删除匹配
蟒蛇
main = ['text \fc + \fr this is my match1 \fc* text', 'text \fc + \fr this is my match2 \fc* text', 'text', 'text', 'text \fc + \fr this is my match \fc* text']
new = []
def rematch(pattern, inp):
matcher = re.compile(pattern)
matches = matcher.match(inp)
if matches:
new.append(matches)
#remove match from "main" somehow?
for x in main:
for m in rematch('\\fc \+ \\fr(.*?)\\fc\*', x):
结果:
main = ['text text', 'text text', 'text', 'text', 'text text']
new = ['this is my match1', 'this is my match2', 'this is my match3']
有没有办法做多个模式匹配? – user1898657 2013-02-21 15:31:01
我不跟着你。你能详细说明你在找什么吗?也许有一个例子? – unutbu 2013-02-21 15:33:34
我想通了,我感谢这个精心制作和美丽的解决方案:) – user1898657 2013-02-21 15:34:46