Tutorial 7 : Regular Expressions in Python- understanding regex flags

Gaurav Patil
3 min readOct 4, 2021
Photo by Ilya Pavlov on Unsplash

Missed previous tutorial ? — Click here

In this tutorial we will cover important flags, important in the sense that they more widely used. To cover flags topic in its entirety, you can visit the regex documentation.

We will cover three flags:

1. re.I (re.IGNORECASE)

2. re.M (re.MULTILINE)

3. re.s (re.DOTALL)

1. re.I (re.IGNORECASE)

This flag is used to perform case-insensitive matching in the given text.

2. re.M (re.MULTILINE)

This flag is used for both ‘^’ and ‘$’ metacharacters in regex.

By default, ‘^’ matches at the beginning of the first line only in case of multiline text. This re.MULTILINE flag enables to do multiline matching.

By default, ‘$’ matches at the end of the last line only in case of multiline text. This re.MULTILINE flag enables to do multiline matching.

3. re.s (re.DOTALL)

This flag is used for ‘ . ’(dot) metacharacter in regex. This flag makes the ‘ . ‘ special character match any character at all, including a newline; without this flag, ‘ . ’will match anything except a newline.

Congratulations !!

Take a deep breathe and move to the next tutorial.

Happy learning !!!

--

--

Gaurav Patil

Machine Learning Engineer drawing insights from mathematics.