我试图在沙捞越时 with,但必须满足任何特性,避免连续三个或三个以上的 com子或半殖民地。 换言之,只允许连续两度 com或半殖民地。
因此,这是我目前拥有的:
^(,|;){,2}([^,;]+(,|;){,2})*$
似乎按预期开展工作:
>>> r.match( )
<_sre.SRE_Match object at 0x7f23af8407e8>
>>> r.match( foo, )
<_sre.SRE_Match object at 0x7f23af840750>
>>> r.match( foo, a )
<_sre.SRE_Match object at 0x7f23af8407e8>
>>> r.match( foo, , )
<_sre.SRE_Match object at 0x7f23af840750>
>>> r.match( foo, ,,a )
<_sre.SRE_Match object at 0x7f23af8407e8>
>>> r.match( foo, ,,, )
>>> r.match( foo, ,,,; )
>>> r.match( foo, ,, ;; )
<_sre.SRE_Match object at 0x7f23af840750>
但是,随着我开始增加投入案文的篇幅,似乎需要更多时间来作出答复。
>>> r.match( foo, bar, baz,, foo )
<_sre.SRE_Match object at 0x7f23af8407e8>
>>> r.match( foo, bar, baz,, fooooo, baaaaar )
<_sre.SRE_Match object at 0x7f23af840750>
>>> r.match( foo, bar, baz,, fooooo, baaaaar, )
<_sre.SRE_Match object at 0x7f23af8407e8>
>>> r.match( foo, bar, baz,, fooooo, baaaaar,, )
<_sre.SRE_Match object at 0x7f23af840750>
>>> r.match( foo, bar, baz,, fooooo, baaaaar,,, )
>>> r.match( foo, bar, baz,, fooooo, baaaaar,,,, )
>>> r.match( foo, bar, baz,, fooooo, baaaaar, baaaaaaz,,,, )
And finally it gets completely stuck at this stage and the CPU usage goes up to 100%.
I m not sure if the regexp could be optimized or there s something else involved, any help appreciated.