<p>你可以选择:</p>
<pre><code>(?| # a so called "branch reset", only supported by the regex module
c\ # a "c "
(?P<catcher>.{2,}?) # at least two characters, lazily -> group "catcher"
\ b\ # followed by " b "
| # or
c\ & \ b\ # "c & b "
(?P<catcher>.+) # capture the rest of the string -> group "catcher"
)
</code></pre>
<p/><hr/>
在<code>Python</code>代码中:
<pre><code># the newer regex module
import regex as re
rx = re.compile(r'''
(?|
c\
(?P<catcher>.{2,}?)
\ b\
|
c\ & \ b\
(?P<catcher>.+))
''', re.VERBOSE)
sampletext = """
c Soumya Sarkar b Rubel Hossain
c Imrul Kayes b Mosaddek Hossain
c & b Sodhi
c Anderson b Boult
"""
catchers = [m.group('catcher') for m in rx.finditer(sampletext)]
print(catchers)
# ['Soumya Sarkar', 'Imrul Kayes', 'Sodhi', 'Anderson']
</code></pre>
<p>看看<a href="https://regex101.com/r/koe73z/2" rel="nofollow noreferrer"><strong>working on regex101.com</strong></a>。<br/>
您需要有较新的<code>regex</code>模块(<code>pip install regex</code>)才能使此工作正常。你知道吗</p>