In [33]: import re
In [34]: pat = re.compile('\\fc \+ \\fr(.*?)\\fc\*')
In [43]: main, new = zip(*[(''.join(parts[::2]), ''.join(parts[1::2])) for parts in [pat.split(m) for m in main]])
In [44]: new = [n.strip() for n in new if n]
In [45]: main
Out[45]: ('text text', 'text text', 'text', 'text', 'text text')
In [46]: new
Out[46]: ['this is my match1', 'this is my match2', 'this is my match']
解释:
注意使用时会发生什么pat.split
:
In [37]: pat.split(main[0])
Out[37]: ['text ', ' this is my match1 ', ' text']
这与您想要的类似,只是您需要 中的奇数项main
和 中的偶数项new
。我们稍后会谈到。
首先,让我们应用pat.split
到 中的每个项目main
:
In [51]: [pat.split(m) for m in main]
Out[51]:
[['text ', ' this is my match1 ', ' text'],
['text ', ' this is my match2 ', ' text'],
['text'],
['text'],
['text ', ' this is my match ', ' text']]
接下来,让我们将奇数项与偶数项分开,并使用''.join
将这些项组合成一个字符串:
In [52]: [(''.join(parts[::2]), ''.join(parts[1::2])) for parts in [pat.split(m) for m in main]]
Out[52]:
[('text text', ' this is my match1 '),
('text text', ' this is my match2 '),
('text', ''),
('text', ''),
('text text', ' this is my match ')]
从这里,我们可以使用zip(*...)
to 分开:main
new
In [53]: main, new = zip(*[(''.join(parts[::2]), ''.join(parts[1::2])) for parts in [pat.split(m) for m in main]])
In [54]: main
Out[54]: ('text text', 'text text', 'text', 'text', 'text text')
In [55]: new
Out[55]: (' this is my match1 ', ' this is my match2 ', '', '', ' this is my match ')