擅长:python、mysql、java
<p>这就是正则表达式的典型用途:查找模式。然后,您可以尝试:</p>
<pre><code>import re # use regexp
original_string = "TwasTheNightBeforeChristmasWhenAllThroughTheHouse"
r = re.compile(".hri.t.asW.en") # constructs the search machinery
res = r.search(original_string) # search
print (res.group(0)) # get results
</code></pre>
<p>结果将是:</p>
<pre><code>ChristmasWhen
</code></pre>
<p>现在,如果您的输入(搜索字符串)必须使用“-”作为通配符,则可以将其转换为正确的正则表达式:</p>
<pre><code>import re
original_string = "TwasTheNightBeforeChristmasWhenAllThroughTheHouse"
s = ".hri.t.asW.en" # supposedly inputed by user
s = s.replace('-','.') # translate to regexp syntax
r = re.compile(s)
res = r.search(original_string)
print (res.group(0))
</code></pre>