[英]Regex, find first - Python
i="<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x2887828> >]], [[[41, 183], 'Button', <wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x28879d0> >]]]"
m = re.findall("<wx.(.*)> >", i)
will give me 会给我的
["<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x2887828> >]], [[[41, 183], 'Button', <wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x28879d0> >"]
However I want it to give me, 不过我想要它给我,
["<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x2887828> >","<wx._controls.Button; proxy of <Swig Object of type 'wxButton *' at 0x28879d0> >"]
The regex is searching all the way until the end, I would like to take all the parts out that match the regex, Does anyone know a solution to this? 正则表达式一直在搜索直到结束,我想把所有与正则表达式匹配的部分拿出来,有没有人知道这个的解决方案?
the *
operator is greedy by default. *
运算符默认是贪心的。 You can change this by adding a ?
你可以通过添加一个来改变它
?
after it. 在它之后。 Also remember to quote the literal dot.
还记得引用字面点。
I also made the group non-matching, otherwise you wouldn't get the desired output (this seems to be a problem with your original code as well): 我也使组不匹配,否则你不会得到所需的输出(这似乎是你的原始代码的问题):
re.findall(r"<wx\.(?:.*?)> >", i)
Another possiblity would be the following (assuming that exactly one <
character comes before the first >
), which is faster than the version with the lazy *
operator: 另一种可能性如下(假设只有一个
<
字符位于第一个>
之前),这比使用lazy *
运算符的版本更快:
re.findall(r"<wx\.[^<]*<[^<]*> >", i)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.