擅长:python、mysql、java
<p>在您链接的示例中,开始和结束处的斜杠是模式的一部分,因此它们与答案的regex部分无关。你知道吗</p>
<p>如果我理解你的问题,<code>pattern</code>将始终是连续的。在这种情况下,不管后面有多少空格,你总能找到它。如果要确保在捕获空白时找到<code>pattern</code>,请使用</p>
<pre><code>import re
lines = 'hello\nhello \n'
pattern = 'hello'
results = re.findall(pattern + r'\s*\n', lines)
print(results)
>>> ['hello\n', 'hello \n']
</code></pre>
<p>如果您不关心空格,只需搜索<code>pattern</code>。你知道吗</p>