擅长:python、mysql、java
<p>您的正则表达式模式:</p>
<pre><code>pattern = r"""
\d+ # 1 or more digits
\. # Escaped period character
\s+? # 1 or more whitespace
(\w+) # 1 or more alphabetic characters
\s+ # 1 or more whitespace
- # hyphen
.* # zero or more of anything besides newline.
"""
</code></pre>
<p>字符串列表:</p>
<pre><code>words = [ "1. hello - jeff", "2. gello - meff", "3. fellow - gef", "12. willow - left"]
for word in words:
# capture results in a variable
# re.X for verbose pattern format.
tmp = re.search(pattern, word, flags = re.X)
# If variable is not None, print results of the first captured group.
if tmp:
print(tmp.group(1))
</code></pre>
<p>输出:</p>
<pre><code>hello
gello
fellow
willow
</code></pre>