How to access several extractions of regular-expression's repeated group in python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

How to access several extractions of regular-expression's repeated group in python?

Suppose we have an IP address "123.45.67.89" and a regex "^(\d{1,3})(\.(\d{1,3})){3}

quot;. We can access the first byte of the address via match.group(1) How can we access the remaining three bytes? They should be packed into match.group(3) somehow.

13th Oct 2018, 2:29 PM
Gyorgy Theisz
Gyorgy Theisz - avatar
5 Answers
+ 2
For some reason curly backets do not work as usual in python. So, maybe like that... where you ask for each group. import re a="123.45.67.89" r=r"(\d+)\.(\d+)\.(\d+)\.(\d+)" s=re.match(r,a) print(s.group(1)) print(s.group(2)) print(s.group(3)) print(s.group(4)) print(s.groups()) # Output # 123 # 45 # 67 # 89 # ('123', '45', '67', '89')
14th Oct 2018, 1:55 AM
blackfish
blackfish - avatar
+ 3
First: thank you, blackfish to point me to the right direction that it may not be possible. I learned that most regex engines capture only the last match for each group, even for repeated ones. So does python, perl, php, JS etc. The biggest exception is the .Net engine, which has a Captures collection for this purpose. I was using it before that's why I was looking for python's solution. And there is a way to access this functionality in python as well, using a third party regex engine: https://pypi.org/project/regex/ After installing I could do the following: import regex m=regex.match("^(\d+)(\.(\d+)){3}
quot;, "123.45.67.89") print(m.captures(3)) ['45', '67', '89']
16th Oct 2018, 10:18 AM
Gyorgy Theisz
Gyorgy Theisz - avatar
+ 2
And why do not use a simple split in this case? a="123.45.67.89" print(a.split(".")) # Output # ['123', '45', '67', '89']
14th Oct 2018, 2:01 AM
blackfish
blackfish - avatar
+ 1
Hi blackfish, thank you for your answer. It is clear for me that all what regexes can do can be implemented programmatically too. I was just curious for regex way. Anyway I liked your answers' simplicity! :)
14th Oct 2018, 7:33 AM
Gyorgy Theisz
Gyorgy Theisz - avatar
0
szia
26th Oct 2018, 8:12 AM
Ragnar Ragnar
Ragnar Ragnar - avatar