擅长:python、mysql、java
<p>要计算字符数,可以使用正则表达式删除任何非字母数字字符,例如:</p>
<pre><code>import re
print(re.sub("[\W]", "", text))
</code></pre>
<p>您也可以使用<code>re</code>模块来计算字数,方法是计算从非字母数字字符处拆分字符串得到的非空字符串:</p>
<pre><code>print([word for word in re.split("[\W]", text) if len(word) > 0])
</code></pre>
<p>如果你也想去掉数字,就用<code>[\W\d]</code>代替<code>[\W]</code>。你知道吗</p>
<p>您可以在正则表达式<a href="http://docs.python.org/2/howto/regex.html" rel="nofollow">here</a>上找到更多信息。你知道吗</p>