Python中的正则表达式很慢?
我想要匹配这些类型的字符串
{@csm.foo.bar}
但不想匹配这些
{@csm.foo.bar-@csm.ooga.booga}
{@csm.foo.bar-42}
我使用的正则表达式是
r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)\}"
如果字符串中有多个匹配项,它的运行速度会变得非常慢。为什么呢?如果我去掉大括号的匹配,速度就会很快,像这样
r"@csm.((?:[a-zA-Z0-9_]+\.?)+)"
但这不是我想要的。
有什么建议吗?
以下是示例输入:
<dockLayout id="popup" y="0" x="0" width="{@csm.screenWidth}" height="{@csm.screenHeight}">
<dataNumber id="selopacity_Volt" name="selopacity_Volt" value="0" />
<dataNumber id="selopacity_Amp" name="selopacity_Amp" value="0" />
<animate trigger="{@m_ds_ML.VIMPBM_BatteryVoltage.valstr}" triggerOn="*" targetNode="selopacity_Volt" targetAttr="value" to="1" dur="0ms" ease="in" />
<animate trigger="{@m_ds_ML.VIMPBM_BatteryVoltage.valstr}" triggerOn="65024" targetNode="selopacity_Volt" targetAttr="value" to="0" dur="0ms" ease="in" />
<animate trigger="{@m_ds_ML.VIMPBM_BatteryCurrent.valstr}" triggerOn="*" targetNode="selopacity_Amp" targetAttr="value" to="1" dur="0ms" ease="in" />
<animate trigger="{@m_ds_ML.VIMPBM_BatteryCurrent.valstr}" triggerOn="65024" targetNode="selopacity_Amp" targetAttr="value" to="0" dur="0ms" ease="in" />
<dockLayout id="item" width="{@csm.screenWidth}" height="{@csm.screenHeight}" depth="-1" clip="false" xmlns="http://www.tat.se/kastor/kml" >
<dockLayout id="list_item_title" x="0" width="{@csm.screenWidth}" height="{@csm.Gearselection.text_heght-@csm.pageVisualCP_y}">
<text id="volt_amp_text" x="0" ellipsize="false" font="{@csm.listUnselFont}" color="{@csm.itemUnselColor}" dockLayout.halign="left" dockLayout.valign="bottom" string="{ItemTitle}" />
</dockLayout>
<dockLayout id="gear_layout" y="0" x="0" width="{@csm.screenWidth}" height="{@csm.vmImage_y_gearselection-@csm.pageVisualCP_y}">
<image id="battery_image" x="0" dockLayout.halign="left" dockLayout.valign="bottom" opacity="1" src="{@m_MenuModel.Gauges.VoltAmpereMeter.image}"/>
</dockLayout>
<!--DockLayout for Voltage Value-->
<dockLayout id="volt_value" x="0" width="{@csm.VoltAmpereMeter.volt_value_x-@csm.VoltAmpereMeter.List_x}" height="{@csm.vmImage_y_gearselection-@csm.pageVisualCP_y}">
<text id="volt_value_text" x="0" opacity="{selopacity_Volt*selopacity_Amp}" ellipsize="false" font="{@csm.listUnselFont}" color="{@csm.itemSelColor}" dockLayout.halign="right" dockLayout.valign="bottom" string="{@m_ds_ML.VIMPBM_BatteryVoltage.valstr}" >
</text>
</dockLayout>
<!--DockLayout for Voltage Unit-->
<dockLayout id="volt_unit" x="{@csm.VoltAmpereMeter.volt_unit_x-@csm.VoltAmpereMeter.List_x}" width="{@csm.screenWidth}" height="{@csm.vmImage_y_gearselection-@csm.pageVisualCP_y}">
<text id="volt_unit_text" x="0" opacity="{selopacity_Volt*selopacity_Amp}" ellipsize="false" font="{@csm.listUnselFont}" color="{@csm.itemSelColor}" dockLayout.halign="left" dockLayout.valign="bottom" string="V" >
</text>
</dockLayout>
<!--DockLayout for Ampere Value-->
<dockLayout id="ampere_value" x="0" width="{@csm.VoltAmpereMeter.ampere_value_x-@csm.VoltAmpereMeter.List_x}" height="{@csm.vmImage_y_gearselection-@csm.pageVisualCP_y}">
<text id="ampere_value_text" x="0" opacity="{selopacity_Amp*selopacity_Volt}" ellipsize="false" font="{@csm.listUnselFont}" color="{@csm.itemSelColor}" dockLayout.halign="right" dockLayout.valign="bottom" string="{@m_ds_ML.VIMPBM_BatteryCurrent.valstr}" >
</text>
</dockLayout>
<!--DockLayout for Ampere Unit-->
<dockLayout id="ampere_unit" x="{@csm.VoltAmpereMeter.ampere_unit_x-@csm.VoltAmpereMeter.List_x}" width="{@csm.screenWidth}" height="{@csm.vmImage_y_gearselection-@csm.pageVisualCP_y}">
<text id="ampere_unit_text" x="0" opacity="{selopacity_Amp*selopacity_Volt}" ellipsize="false" font="{@csm.listUnselFont}" color="{@csm.itemSelColor}" dockLayout.halign="left" dockLayout.valign="bottom" string="A" >
</text>
</dockLayout>
<!--DockLayout for containing Data Not Available text-->
<dockLayout id="no_data_textline" x="{@csm.VoltAmpereMeter.List_x1-@csm.VoltAmpereMeter.List_x}" width="{@csm.screenWidth}" height="{@csm.vmImage_y_gearselection-@csm.pageVisualCP_y}">
<text id="no_data_text" x="0" opacity="{1-(selopacity_Amp*selopacity_Volt)}" ellipsize="false" font="{@csm.listSelFont}" color="{@csm.itemSelColor}" dockLayout.halign="left" dockLayout.valign="bottom" string="{text1}" >
</text>
</dockLayout>
<!--<rect id="test_rect1" x="{151-28}" y="0" width="1" height="240" opacity="1" fill="#00ff00" />
<rect id="test_rect1" x="{237-28}" y="0" width="1" height="240" opacity="1" fill="#00ff00" />
<rect id="test_rect1" x="{160-28}" y="0" width="1" height="240" opacity="1" fill="#00ff00" />
<rect id="test_rect1" x="{246-28}" y="0" width="1" height="240" opacity="1" fill="#00ff00" />
<rect id="test_rect8" x="0" y="{161-40}" width="320" height="1" opacity="1" fill="#00ff00" />
<rect id="test_rect1" x="{109-28}" y="0" width="1" height="240" opacity="1" fill="#00ff00" />-->
</dockLayout>
</dockLayout>
3 个回答
0
你可能需要给出一个更好的例子,具体说明什么地方慢了。比如说,对于一个比较长的字符串,其中有些部分是匹配的,有些部分不是:
x="".join(['{@csm.foo.bar-%d}\n{@csm.foo.%dx.baz}\n' % (a,a)
for a in xrange(10000)])
mymatch=r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)\}"
for y in re.finditer(mymatch,x):
print y.group(0)
这样是没问题的,但如果你的字符串很长,而且搜索的方法不太好,就可能会遇到问题。
0
我对正则表达式不是特别精通,但可能是因为匹配的最后有个大括号。你可以试着匹配 r"\{@csm.((?:[a-zA-Z0-9_]+\.?)+)"
,然后手动检查一下最后有没有闭合的大括号。
4
你能提供一个字符串的测试案例,让它的第一个匹配结果是“dog slow”吗?顺便说一下,虽然我不知道这是否会影响性能,但这个正则表达式有点不精确——它在{@csm
开头后可以匹配任何单个字符,而不仅仅是一个点;也许可以用一个更好的表达式(可能更快,因为它不把点“变成可选”)来替代:
r'\{@csm((?:\.\w+)+)\}'