Regular Expression – Grouping

In this lesson, we will learn how to use grouping in the regular expression. Grouping is basically used if we want to extract sub-patterns from the larger part of the match.

Let’s consider the situation in which we have a list of the date of birth of all the employees of a company and we only want the month and year from the dates. This can be achieved by using a regular expression with grouping to match all the dates and then from those extracted matched dates we can further extract different components such as day, month, and year.

For using the grouping in regular expression we have to place our pattern inside of the parenthesis.

For example, let’s assume we have a source string: “Ramesh’s date of birth is 18/02/1980”.

Now, if we want to extract month and year from the above string we have to write a regex pattern like the below:

pattern = "\d{1,2}/(\d{1,2})/(\d{4})"

Here, we have placed parenthesis around the pattern of month and year because we have to extract them from the dates. Let’s see the code below:

Regular_Expression_Basics1

Now as we can see from the above output, group(0) contains the default match i.e., the whole match based on the regex pattern whereas group(1) and group(2) contain the match for the month and the year respectively.

Let’s practice one more exercise to understand grouping better.

Exercise 1

Description

Write a regular expression to extract the domain name from an email address present in a raw string.

Sample input: 
wisdomml2020@gmail.com 

Expected output: 
gmail.com

Regular_Expression_Basics1