擅长:python、mysql、java
<pre><code>import re
phoneRegex = re.compile("(\({0,1}[\d]{3}\)(?=[\d]{3})|[\d]{3}-)([\d]{3}[-]{0,1}[\d]{4})")
numbers = ["123-333-1111", "(123)111-2222", "123-2221111", "1232221111", "(123)-111-2222"]
for number in numbers:
print bool(re.match(phoneRegex, number))
</code></pre>
<p><strong>输出</strong></p>
<pre><code>True
True
True
False
False
</code></pre>
<p>您可以在这里看到对这个正则表达式的解释:<a href="http://regex101.com/r/bA4fH8" rel="nofollow">http://regex101.com/r/bA4fH8</a></p>