Claus Spitzer
Claus Spitzer

Reputation: 34

MicroPython Regex not matching although it does online

I have a strange Problem. When I parse my Regex online it works fine, but in MicroPython doesn't match it.

regex: ()*<div>(.*?)<\/div>()*or<div>(.*?)<\/div>or<div>(.*?)</div>

toMatch:

&lt;Storage {}&gt;86400<div>Uhrzeit in Sekunden: 65567</div><div>Timer: 20833</div>

none of these match with python but do online (http://regexr.com/ or https://pythex.org/)

This is just a short part of what i want to get. But what i want is the data inside the div.

EDIT: I am using micropython on a esp8266. I am limited and cant use a html parser.

Upvotes: 1

Views: 1106

Answers (1)

BoarGules
BoarGules

Reputation: 16942

I suspect your problem is that you are not passing a raw string to re.compile(). If I do this I get what I think you want:

>>> rx = re.compile(r"<div>(.*?)<\/div>")
>>> rx.findall("&lt;Storage {}&gt;86400<div>Uhrzeit in Sekunden: 65567</div><div>Timer: 20833</div>")
>>> ['Uhrzeit in Sekunden: 65567', 'Timer: 20833']

You need a raw string because \ is both the Python string escape character and the regex escape character. Without it you have to put \\ in your regex when you mean \ and that very quickly becomes confusing.

Upvotes: 2

Related Questions