Match/Search#
This section discusses options for working with re.Match objects, which are generally generated by match and search operations.
import re
Groups#
You can define groups in patterns using (). All values surrounded by brackets will be considered a group, allowing you to extract specific sections of the string that match the pattern you are processing. After getting re.Match object you can load those groups by using group method.
The following cell defines an example where there is useful information that have to be loaded, surrounded by the different types of brackets.
m = re.match(
r'\[(.*?)\] - \{(.*?)\} - \((.*?)\)',
"[group1] - {group2} - (group3)"
)
If you call the group method without arguments or with 0 you would just get a whole substring matching your regular expression.
m.group()
'[group1] - {group2} - (group3)'
m.group(0)
'[group1] - {group2} - (group3)'
By specifying the number, you will get the parts of the string that correspond to the group in the appropriate order.
m.group(1), m.group(2), m.group(3)
('group1', 'group2', 'group3')