擅长:python、mysql、java
<p>可以使用正则表达式检查字符串是否只包含数字、连字符或斜杠。</p>
<pre><code>>>> bool(re.match('[\d/-]+$', '2015-07-01'))
True
>>> bool(re.match('[\d/-]+$', '2015-Jul-01'))
False
</code></pre>
<p>这里不需要^锚,因为<code>match</code>从字符串的开头开始。</p>
<p>或者,使用<code>all</code>而不使用regex:</p>
<pre><code>>>> from string import digits
>>> allowed = set(digits).union('/-')
>>> all(c in allowed for c in '2015-07-01')
True
>>> all(c in allowed for c in '2015-Jul-01')
False
</code></pre>