Daniel Restrepo
Daniel Restrepo

Reputation: 73

Get span and match from finditer

I want obtain the tuple in span and the str of macht, but i dont know how acces to this object, the methods from the documentation dont work

import re

s = 'sd(asdf(xf))fg'

if re.findall('\([A-z ]+\)', s):

    m = re.finditer('\([A-z ]+\)', s)
    m = list(m)

print(m)

I get this output:

[<_sre.SRE_Match object; span=(7, 11), match='(xf)'>]

What process or method i need for get (7, 11) and '(xf)'?

Upvotes: 3

Views: 2573

Answers (1)

kjmerf
kjmerf

Reputation: 4345

Try it like this:

import re

s = 'sd(asdf(xf))fg'

for x in re.finditer('\([A-z ]+\)', s):
    print((x.start(), x.end()), x.group())

#output

(7, 11) (xf)

Upvotes: 5

Related Questions