擅长:python、mysql、java
<ul>
<li>我需要将正则表达式拆分成多行,否则代码看起来会很糟糕。我该怎么做?在</li>
</ul>
<p>编译正则表达式时使用<a href="http://docs.python.org/library/re.html#re.VERBOSE" rel="nofollow">^{<cd1>}</a>标志。在</p>
<pre><code>pattern = re.compile(r"""(
[\u0985-\u0994] # comment to explain what this is
| [\u0995-\u09B9]
# etc.
)
""", re.VERBOSE)
</code></pre>
<ul>
<li>我想使用某种类型的字符串替换/模板来“命名”Unicode范围</li>
</ul>
<p>可以从普通Python字符串构造RE:</p>
^{pr2}$
<ul>
<li>允许的最小“结构”列表将在以后扩展。有没有办法在正则表达式中设置一种“循环”机制,使其适用于列表中所有允许的结构?在</li>
</ul>
<p>我不知道我是否明白你的意思,但是。。。假设您有一个(未编译)REs的列表,比如,<code>patterns</code>,那么您可以使用</p>
<pre><code>re.compile("(%s)" % "|".join(patterns))
</code></pre>
<p>以这种方式构造REs时要注意特殊字符,必要时使用<a href="http://docs.python.org/library/re.html#re.escape" rel="nofollow">^{<cd3>}</a>。在</p>