【问题标题】:Regex Getting everything in between, Python正则表达式介于两者之间,Python
【发布时间】:2012-05-26 18:18:36
【问题描述】:

为什么这个正则表达式不起作用?

import re
i="<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x2d040b0> >"
m = re.match("controls(.*)[;]", i)
if m:
    print m.group(1)

它什么也不返回。我正在尝试将所有内容置于“控制”和“;”之间 此解决方案适用于其他测试用例,但不适用于这个。

【问题讨论】:

标签: python regex wxpython wxwidgets


【解决方案1】:

re.match 只匹配字符串的开头。你想要re.search

但是,您似乎正在评估 repr 在对象上的结果以获取类名。为什么不直接使用obj.__class__.__name__ 呢?使用 duck typing 并避免使用特定于单个类的代码。

【讨论】:

    【解决方案2】:

    .match() 从头开始​​看。你想要.search()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-15
      • 1970-01-01
      • 2018-08-27
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多