擅长:python、mysql、java
<pre><code>>>> import re
>>> string = 'apple,pear,grapes,carrot.cabbage,veggies.fruit,yard'
>>> re.split(',|\.',string)
['apple', 'pear', 'grapes', 'carrot', 'cabbage', 'veggies', 'fruit', 'yard']
</code></pre>
<p>这将使用转换运算符<code>|</code>在<code>,</code>或<code>.</code>(必须转义为<code>\.</code>)上进行拆分。</p>
<p>它也可以用字符类编写:</p>
<pre><code>>>> re.split('[,.]',string)
['apple', 'pear', 'grapes', 'carrot', 'cabbage', 'veggies', 'fruit', 'yard']
</code></pre>
<p>但这一点不太普遍,因为两个字符都不能用短语代替。</p>