擅长:python、mysql、java
<p>埃尔达拉提斯说的一切都是真的。但是,对于一个名为:<code>'patternalphaonly'</code>的变量,我假设作者希望验证字符串是否只由字母字符组成。如果这是真的,那么我会向正则表达式添加额外的字符串结束锚点,如下所示:</p>
<pre class="lang-py prettyprint-override"><code>patternalphaonly = re.compile('^[a-zA-Z]+$')
result = patternalphaonly.search(numberstring)
</code></pre>
<p>或者,更好的是,因为它只在字符串的开头匹配,所以使用首选的<code>match</code>方法:</p>
^{pr2}$
<p>(由于某些尚未解释的原因,它显然更快。)</p>