Here is my code:
a = '/afolder_l/location/folder_l/file.jpg'
p= re.compile("/.+/location/.+_([lr])/")
m = p.match(a)
Now, print m.group(1) gives l, but I would like the position of the group as well. Right now, m.span() gives a tuple with a position that includes all the text. How could I just get the location of l? Or 'r' since that's what I am trying to group?
group() returns the substring that was matched by the RE. start() and end() return the starting and ending index of the match. span() returns both start and end indexes in a single tuple. Since the match() method only checks if the RE matches at the start of a string, start() will always be zero.
Capturing groups are a handy feature of regular expression matching that allows us to query the Match object to find out the part of the string that matched against a particular part of the regular expression. Anything you have in parentheses () will be a capture group.
You need to reference the group number:
>>> import re
>>>
>>> a = '/afolder_l/location/folder_l/file.jpg'
>>> p= re.compile("/.+/location/.+_([lr])/")
>>> m = p.match(a)
>>> m.span()
(0, 29)
>>> m.span(1)
(27, 28)
You can use .span() method of SRE_Match object with integer argument input as a group number.
Some examples for more clarity. If you use 3 groups of () so the group 0 will be the full matched, and with argument input as integer number from 1 to 3 will be the matched and index of each group number with .group() and .span() method, respectively. Hope this helps!
>>> import re
>>> regex = re.compile(r"(\d{4})\/(\d{2})\/(\d{2})")
>>> text = "2019/12/31"
>>> matched = regex.match(text)
>>> matched
<_sre.SRE_Match object; span=(0, 10), match='2019/12/31'>
>>> matched.groups()
('2019', '12', '31')
>>> matched.span()
(0, 10)
>>> matched.group(0)
'2019/12/31'
>>> matched.span(0)
(0, 10)
>>> matched.group(1)
'2019'
>>> matched.span(1)
(0, 4)
>>> matched.group(2)
'12'
>>> matched.span(2)
(5, 7)
>>> matched.group(3)
'31'
>>> matched.span(3)
(8, 10)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With