0

Regular Expressions Python.

I'm struggling to understand the concept of Groups in Regex, May someone please explain what is group and how can I determine how many groups are in the line of code?

19th Jun 2019, 1:02 PM
Senzo Lorenzo
Senzo Lorenzo - avatar
2 ответов
+ 4
Groups can be used to express that you only want to find a specific part of a string. For example let's say you want to extract names from a string and you assume that every name will appear in a context like "my name is xyz" or "your name is xyz". You don't care about the "my name is" part, you only want to find the names in the string. So you put the name in parentheses () to turn it into a group. Your pattern might look like this: pattern = re.compile(r'name is (\w+)', re.I) If you run this pattern on a string and use the re.findall() method, all groups are returned: s = 'My name is Anna und your name is Lorenzo' print(pattern.findall(s)) # ['Anna', 'Lorenzo'] Here, re.findall() only extracts the names from the string. Without using groups, it would return the whole match: pattern = re.compile(r'name is \w+', re.I) # pattern doesn't have groups print(pattern.findall(s)) # ['name is Anna', 'name is Lorenzo'] Or if you know that e.g. a number you're looking for appears as the second number in a string, you can find it by using .group(2): pattern = re.compile(r'(\d+)\D+(\d+)') s = 'He is 74 years old and weighs 287 kg' weight = pattern.search(s).group(2) print(weight) # 287
19th Jun 2019, 2:22 PM
Anna
Anna - avatar
+ 2
If you want to capture several parts of the matched string, then groups() can be useful as it will give you a tuple containing each subgroup you defined in the regex with round brackets () See an example here: https://code.sololearn.com/cVis1miBgICj/?ref=app
19th Jun 2019, 2:18 PM
Tibor Santa
Tibor Santa - avatar