<p>这应该可以做到:</p>
<pre class="lang-py prettyprint-override"><code>import re
regex = (r'(?<=\s)(\d+)(?=\s|,)') # positive and negative look assertions
# note use of triple quotes to negate use of \
match = re.findall(regex, """Failure: Cannot retrieve market data: [Historical correlation for instruments 48021088, 1029755
is older than 2M, Historical correlation for instruments 48021088, 1029755 is older than 2M, Error while loading Structured Product market data
Failure: Cannot retrieve market data: [Historical correlation for instruments 52598110, 35602558 is older than 2M, Historical correlation for instruments
52598110, 35602558 is older than 2M, Historical correlation for instruments 52598110, 35602558 is older than 2M, Historical correlation for instruments 52598110,
35602558 is older than 2M, Error while loading Structured Product market data
Failure: Cannot retrieve market data: [Historical correlation for instruments 48021088, 1029755 is older than 2M, Historical correlation for instruments 48021088, 1029755
is older than 2M, Error while loading Structured Product market data
Failure: Cannot retrieve market data: [Historical correlation for instruments 612292, 52598110 is older than 2M, Historical correlation for instruments 612292, 52598110 is
older than 2M, Historical correlation for instruments 612292, 52598110 is older than 2M, Historical correlation for instruments 612292, 52598110 is older than 2M,
Error while loading Structured Product market data
Failure: Cannot retrieve market data: [Historical correlation for instruments 489459, 104322960 is older than 2M, Historical correlation for instruments 489459,
104322960 is older than 2M, Historical correlation for instruments 489459, 104322960 is older than 2M, Historical correlation for instruments 489459,
104322960 is older than 2M, Error while loading Structured Product market data""")
unique_numbers = list(set(match))
print(*unique_numbers) # print as required
</code></pre>
<hr/>
<p><strong>解释</strong></p>
<p>从Python <a href="https://docs.python.org/3/library/re.html#re.sub" rel="nofollow noreferrer">Regex docs</a></p>
<p>如果字符串中的当前位置前面有一个以当前位置结尾的<code>...</code>匹配项,则<code>(?<=...)</code>匹配。这被称为正向查找断言</p>
<p>如果<code>...</code>匹配next,则<code>(?=...)</code>匹配,但不使用任何字符串。这称为前瞻断言</p>
<p>这些共同构成了一个环顾四周的断言。然后我们有<code><look behind>(\d+)<look ahead></code>。<code>()</code>表示这是匹配的组-从<code>findall</code>调用返回的组。然后<code>\d+</code>和以前一样-不止一个数字</p>