


We will see how to capture single as well as multiple groups. using the group(group_number) method of the regex Match object we can extract the matching value of each group.

Capturing groups are numbered by counting their opening parentheses from left to right.Ĭapturing 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.Īnything you have in parentheses () will be a capture group. Each sub-pattern inside a pair of parentheses will be captured as a group. We can specify as many groups as we wish. They are created by placing the characters to be grouped inside a set of parentheses (, ).įor example, In the expression, ((\w)(\s\d)), there are three such groups For example, the regular expression (cat) creates a single group containing the letters ‘c’, ‘a’, and ‘t’.įor example, in a real-world case, you want to capture emails and phone numbers, So you should write two groups, the first will search email, and the second will search phone numbers.Īlso, capturing groups are a way to treat multiple characters as a single unit. We create a group by placing the regex pattern inside the set of parentheses ( and ) . A group is a part of a regex pattern enclosed in parentheses () metacharacter.
