Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex Getting everything in between, Python

Why is this regex not working?

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)

It returns nothing. I am trying to get everything in between, "controls" and ";" This solution works with other test cases however not with this one.

like image 806
user1357159 Avatar asked Mar 15 '26 16:03

user1357159


2 Answers

re.match only matches at the beginning of the string. You want re.search.

However, it looks like you're evaluating the result of repr on an object to get the class name. Why not just use obj.__class__.__name__ instead? On use duck typing and avoid code specific to individual classes.

like image 91
robert Avatar answered Mar 17 '26 06:03

robert


.match() looks from the beginning. You want .search().

like image 29
Ignacio Vazquez-Abrams Avatar answered Mar 17 '26 05:03

Ignacio Vazquez-Abrams



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!