<p>我在使用<code>regex sub</code>从字符串中删除数字时遇到一些问题。输入字符串可以如下所示:</p>
<pre><code>"The Term' means 125 years commencing on and including 01 October 2015."
"125 years commencing on 25th December 1996"
"the term of 999 years from the 1st January 2011"
</code></pre>
<p>我要做的是删除数字和单词<code>'years'</code>-我也在使用<code>DateFinder</code>来分析日期字符串,但是{<cd3>}将数字解释为日期-因此我要删除数字。在</p>
<p>对<code>regex</code>表达式删除数字和单词<code>'years'</code>有什么想法吗?在</p>
<p>尝试删除数字和单词<code>years</code>:</p>
<pre><code>re.sub(r'\s+\d+|\s+years', '', text)
</code></pre>
<p>例如,如果:</p>
^{pr2}$
<p>则输出为:</p>
<pre><code>"The Term' means commencing on and including October."
</code></pre>