PYnative

Python Programming

  • Learn Python
  • Exercises
  • Quizzes
  • Code Editor
  • Tricks
Home » Python » RegEx » Python Regex Flags

Python Regex Flags

Updated on: April 13, 2021 | Leave a Comment

Python regex allows optional flags to specify when using regular expression patterns with match(), search(), and split(), among others.

All RE module methods accept an optional flags argument that enables various unique features and syntax variations.

For example, you want to search a word inside a string using regex. You can enhance this regex’s capability by adding the RE.I flag as an argument to the search method to enable case-insensitive searching.

You will learn how to use all regex flags available in Python with short and clear examples.

First, refer to the below table for available regex flags.

Flaglong syntaxMeaning
re.Are.ASCIIPerform ASCII-only matching instead of full Unicode matching
re.Ire.IGNORECASEPerform case-insensitive matching
re.Mre.MULTILINEThis flag is used with metacharacter ^ (caret) and $ (dollar).
When this flag is specified, the metacharacter ^ matches the pattern at beginning of the string and each newline’s beginning (\n).
And the metacharacter $ matches pattern at the end of the string and the end of each new line (\n)
re.Sre.DOTALLMake the DOT (.) special character match any character at all, including a newline. Without this flag, DOT(.) will match anything except a newline
re.Xre.VERBOSEAllow comment in the regex. This flag is useful to make regex more readable by allowing comments in the regex.
re.Lre.LOCALEPerform case-insensitive matching dependent on the current locale. Use only with bytes patterns
Python regex flags

To specify more than one flag, use the | operator to connect them. For example, case insensitive searches in a multiline string

re.findall(pattern, string, flags=re.I|re.M|re.X)

Now let’s see how to use each option flag in Python regex.

Table of contents

  • IGNORECASE flag
  • DOTALL flag
  • VERBOSE flag
  • MULTILINE flag
  • ASCII flag

IGNORECASE flag

First of all, let’s see the re.I flag’s role, which stands for ignoring a case. specified this flag in the regex method as an argument to perform case insensitive matching. You can specify this flag using two ways

  1. re.I
  2. re.IGNORECASE

Example

import re

target_str = "KELLy is a Python developer at a PYnative. kelly loves ML and AI"

# Without using re.I
result = re.findall(r"kelly", target_str)
print(result)
# Output ['kelly']

# with re.I
result = re.findall(r"kelly", target_str, re.I)
print(result)
# Output ['KELLy', 'kelly']

# with re.IGNORECASE
result = re.findall(r"kelly", target_str, re.IGNORECASE)
print(result)
# Output ['KELLy', 'kelly']

Notice the word “kelly” the occurs two times inside this string., First, capitalized at the beginning of the sentences and second in all lowercase.

In the first re.findall() method, we got only one occurrence because, by default, the matching is case sensitive.

And in the second re.findall() method, we got 2 occurrences because we changed the case sensitive behavior of regex using re.I so that it can find all the occurrences of a word regardless of any of its letters being uppercase or lowercase.

DOTALL flag

Now, let’s see the re.S flag’s role. You can specify this flag using two ways

  1. re.S
  2. re.DOTALL

As you know, By default, the dot(.) metacharacter inside the regular expression pattern represents any character, be it a letter, digit, symbol, or a punctuation mark, except the new line character, which is \n.

The re.S flag makes this exception disappear by enabling the DOT(.) metacharacter to match any possible character, including the new line character hence its name DOTALL.

This can prove to be pretty useful in some scenarios, especially when the target string is a multi-line.

Now let’s use the re.search() method with and without the RE.S flag.

Example

import re

# string with newline character
target_str = "ML\nand AI"

# Match any character
result = re.search(r".+", target_str)
print("Without using re.S flag:", result.group())
# Output 'ML'

# With re.S flag
result = re.search(r".+", target_str, re.S)
print("With re.S flag:", result.group())
# Output 'ML\nand AI'

# With re.DOTALL flag
result = re.search(r".+", target_str, re.DOTALL)
print("With re.DOTALL flag:", result.group())
# Output 'ML\nand AI'

In the first call of a re.search() method, DOT didn’t recognize the \n and stopped matching. After adding the re.S option flag in the next call, The dot character matched the entire string.

VERBOSE flag

That re.X flag stands for verbose. This flag allows more flexibility and better formatting when writing more complex regex patterns between the parentheses of the match(), search(), or other regex methods.

You can specify this flag using two ways

  1. re.X
  2. re.VERBOSE

The verbose flag allows us to the following inside the regex pattern

  • Better spacing, indentation, and a clean format for more extended and intricate patterns.
  • Allows us to add comments right inside the pattern for later reference using the hash sign (#).

When to use

For some reason, you feel that the pattern looks complicated. Although it can get way more complicated than this, you can make it prettier and more readable by adding indentation and comments using re.X or re.VERBOSE.

import re

target_str = "Jessa is a Python developer, and her salary is 8000"

# re.X to add indentation  and comment in regex
result = re.search(r"""(^\w{2,}) # match 5-letter word at the start
                        .+(\d{4}$) # match 4-digit number at the end """, target_str, re.X)
# Fiver-letter word
print(result.group(1))
# Output 'Jessa'

# 4-digit number
print(result.group(2))
# Output 8000

MULTILINE flag

You can specify this flag using two ways

  1. re.M
  2. re.MULTILINE

The re.M flag is used as an argument inside the regex method to perform a match inside a multiline block of text.

Note: This flag is used with metacharacter ^ and $.

  1. The caret (^)matches a pattern only at the beginning of the string
  2. The dollar ($) matches the regular expression pattern at the end of the string

When this flag is specified, the pattern character ^ matches at the beginning of the string and each newline’s start (\n). And the metacharacter character $ match at the end of the string and the end of each newline (\n).

Now let’s see the examples.

import re

target_str = "Joy lucky number is 75\nTom lucky number is 25"

# find 3-letter word at the start of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"^\w{3}", target_str)
print(result)  
# Output ['Joy']

# find 2-digit at the end of each newline
# Without re.M or re.MULTILINE flag
result = re.findall(r"\d{2}$", target_str)
print(result)
# Output ['25']

# With re.M or re.MULTILINE
# find 3-letter word at the start of each newline
result = re.findall(r"^\w{3}", target_str, re.MULTILINE)
print(result)
# Output ['Joy', 'Tom']

# With re.M
# find 2-digit number at the end of each newline
result = re.findall(r"\d{2}$", target_str, re.M)
print(result)
# Output ['75', '25']

ASCII flag

You can specify this flag using two ways

  1. re.A
  2. re.ASCII

Make regex  \w, \W, \b, \B, \d, \D, \s and \S perform ASCII-only matching instead of full Unicode matching. This is only meaningful for Unicode patterns and is ignored for byte patterns.

import re

# string with ASCII and Unicode characters
target_str = "虎太郎 and Jessa are friends"

# Without re.A or re.ASCII
# To match all 3-letter word
result = re.findall(r"\b\w{3}\b", target_str)
print(result)
# Output ['虎太郎', 'and', 'are']

# With re.A or re.ASCII
# regex to match only 3-letter ASCII word
result = re.findall(r"\b\w{3}\b", target_str, re.A)
print(result)
# Output ['and', 'are']

Previous:

Python Regex special sequences

Next:

Python Regex Metacharacters

Filed Under: Python, Python RegEx

Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.

TweetF  sharein  shareP  Pin

About Vishal

Founder of PYnative.com I am a Python developer and I love to write articles to help developers. Follow me on Twitter. All the best for your future Python endeavors!

Related Tutorial Topics:

Python Python RegEx

Python Exercises and Quizzes

Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.

  • 15+ Topic-specific Exercises and Quizzes
  • Each Exercise contains 10 questions
  • Each Quiz contains 12-15 MCQ
Exercises
Quizzes

Leave a Reply Cancel reply

your email address will NOT be published. all comments are moderated according to our comment policy.

Use <pre> tag for posting code. E.g. <pre> Your entire code </pre>

Posted In

Python Python RegEx
TweetF  sharein  shareP  Pin

  Python RegEx

  • Python RegEx
  • Python regex compile
  • Python regex match
  • Python regex search
  • Python regex findall
  • Python regex split
  • Python regex replace
  • Python regex capturing groups
  • Regex Metacharacters
  • Regex special sequences
  • Regex Flags

All Python Topics

Python Basics Python Exercises Python Quizzes Python File Handling Python OOP Python Date and Time Python Random Python Regex Python Pandas Python Databases Python MySQL Python PostgreSQL Python SQLite Python JSON

About PYnative

PYnative.com is for Python lovers. Here, You can get Tutorials, Exercises, and Quizzes to practice and improve your Python skills.

Explore Python

  • Learn Python
  • Python Basics
  • Python Databases
  • Python Exercises
  • Python Quizzes
  • Online Python Code Editor
  • Python Tricks

Follow Us

To get New Python Tutorials, Exercises, and Quizzes

  • Twitter
  • Facebook
  • Sitemap

Legal Stuff

  • About Us
  • Contact Us

We use cookies to improve your experience. While using PYnative, you agree to have read and accepted our Terms Of Use, Cookie Policy, and Privacy Policy.

Copyright © 2018–2023 pynative.com