Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python regex get group position

Tags:

python

regex

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?

like image 346
Arvizard Avatar asked Mar 28 '16 03:03

Arvizard


People also ask

What is Groups () in Python?

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.

What is capturing group in regex Python?

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.


2 Answers

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)
like image 136
alecxe Avatar answered Nov 15 '22 20:11

alecxe


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)
like image 22
RedNam Avatar answered Nov 15 '22 20:11

RedNam