擅长:python、mysql、java
<p><a href="https://stackoverflow.com/a/8802889/1138689">RanRag has already answered</a>这是你的具体问题。</p>
<p>然而,更一般地说,你在做什么</p>
<pre><code>if [[ "$string" =~ ^hello ]]
</code></pre>
<p>是一个<em>regex</em>匹配项。要在Python中执行同样的操作,您可以执行以下操作:</p>
<pre><code>import re
if re.match(r'^hello', somestring):
# do stuff
</code></pre>
<p>显然,在这种情况下,<code>somestring.startswith('hello')</code>更好。</p>