1

I'm trying to convert a perl regex to python equivalent.

Line in perl:

($Cur) = $Line =~ m/\s*\<stat\>(.+)\<\/stat\>\s*$/i;

What I've attempted, but doesn't seem to work:

m = re.search('<stat>(.*?)</stat>/i', line)
cur = m.group(0)
5
  • 3
    Apart from anything else, you need to swap the arguments around: re.search('<stat>(.*?)</stat>/i', line). Commented Oct 29, 2013 at 21:52
  • 5
    + means the same thing in Perl and Python, I'm not sure why you'd change (.+) to (.*?). Commented Oct 29, 2013 at 21:55
  • Those two regex aren't equivalent. Commented Oct 30, 2013 at 8:20
  • @kylex Are there still problems not addressed by the answers? Commented Oct 31, 2013 at 16:39
  • @sweeneyrod, nope, I've marked an answer. Commented Oct 31, 2013 at 16:42

2 Answers 2

5

almost /i means case insensitive

m = re.search(r'<stat>(.*?)</stat>',line,re.IGNORECASE)

also use the r modifier on the string so you dont need to escape stuff like angle brackets.

but my guess is a better solution is to use an html/xml parser like beautifulsoup or other similar packages

Sign up to request clarification or add additional context in comments.

Comments

3

Something like the following ...

r is Python’s raw string notation for regex patterns and to avoid escaping, after the prefix comes your regular expression following your string data. re.I is used for case-insensitive matching.

See the re documentation explaining this in more detail.

To find your match, you could use the group() method of MatchObject like the following:

cur = re.search(r'<stat>([^<]*)</stat>', line).group(1)

Using search() matches only the first occurrence, use findall() to match all occurrences.

matches = re.findall(r'<stat>([^<]*)</stat>', line)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.