+2

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}$". 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.

10/13/2018 2:29:36 PM

Gyorgy Theisz

5 Answers

New Answer

+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')

+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']

+2

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}$", "123.45.67.89") print(m.captures(3)) ['45', '67', '89']

0

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! :)

0