擅长:python、mysql、java
<p>我宁愿搜索所有独立的数字,而不是试图去掉所有的字母字符。这里有一个选项,将<code>re.findall</code>与regex模式<code>\b\d+\b</code>一起使用:</p>
<pre><code>text = ['C1412DRE, New York 2695','Direction 12','Main Street 6254 C13D']
inp = ' '.join(text)
matches = re.findall(r'\b\d+\b', inp)
print(matches)
['2695', '12', '6254']
</code></pre>
<p>我的逻辑中可能存在的一个缺陷是,我将您的字符串列表合并为一个字符串,用空格分隔。这是必需的,因为<code>re.findall</code>需要单个字符串进行搜索。但是,由于regex模式只寻找已经被单词边界分隔的数字,所以按空格连接不应该引入任何副作用(我认为)。你知道吗</p>