Menu Close

Change the behaviour of the Regular Expression using flags

Python makes regular expressions available through the re module.

Basics of Regular Expressions in Python, check the link below ๐Ÿ‘‡ ๐Ÿ‘‡
Regular Expressions Regex basic in Python ๐Ÿ‘ˆ ๐Ÿ‘ˆ

Also with table of contents about symbols and their usage in Regex

Flags in Regex

For some special cases we need to change the behaviour of the Regular Expression, this is done using flags. Flags can be set in two ways, through the flags keyword or directly in the expression.

Flags keyword

Below an example for re.search but it works for most functions in the re module.

import re
m = re.search("b", "ABC")
print(m is None)
# Output: True

m = re.search("b", "ABC", flags=re.IGNORECASE)
print(m.group())
# Output: 'B'

m = re.search("a.b", "A\nBC", flags=re.IGNORECASE)
print(m is None)
# Output: True

m = re.search("a.b", "A\nBC", flags=re.IGNORECASE|re.DOTALL)
print(m.group())
# Output: 'A\nB'

OUTPUT: (WHEN EXECUTING THE ABOVE CODE)
True
B
True
A
B


ILLUSTRATION OVER A PYTHON3 CLI

Executed using python3 command line terminal LINUX


Common Flags
Flag                                                   Short Description
re.IGNORECASE,  re.I               Makes the pattern ignore the case
re.DOTALL,  re.S                        Makes . match everything including newlines
re.MULTILINE, re.M                   Makes ^ match the begin of a line and $ the end of a line
re.DEBUG                                  Turns on debug information

For the complete list of all available flags check the docs

Inline flags

From the docs:

(?iLmsux) (One or more letters from the set ‘i’, ‘L’, ‘m’, ‘s’, ‘u’, ‘x’.)

The group matches the empty string; the letters set the corresponding flags: re.I (ignore case), re.L (locale dependent), re.M (multi-line), re.S (dot matches all), re.U (Unicode dependent), and re.X (verbose), for the
entire regular expression. This is useful if you wish to include the flags as part of the regular expression,
instead of passing a flag argument to the re.compile() function.


Note that the (?x) flag changes how the expression is parsed. It should be used first in the expression
string, or after one or more whitespace characters. If there are non-whitespace characters before the flag,
the results are undefined.

For the first part in using regular expressions and matching the string
you can visit this link โ€“> Matching the beginning of a string (Regex) Regular Expressions in python

The re.search() method takes a regular expression pattern and a string and searches for that pattern within the string. If the search is successful, search() returns a match object or None otherwise.
For the second part in using regular expression and searching the string
visit โ€“> Searching โ€“ Regular Expressions (Regex) in Python

Precompiled_pattern —> Precompiled patterns โ€“ Regular Expression(Regex) in Python
Compiling a pattern allows it to be reused later on in a program.
However, note that Python caches recently-used expressions, so โ€œprograms that use only a few regular expressions at a time neednโ€™t worry about compiling regular expressionsโ€.


Morae Q!

  1. Program to get dictionary items, use update module and items module.
  2. Regular expression allowing certain set of characters.
  3. Compute the Sum of Cosine Series.
  4. Find the Sum of Sine Series.
  5. Remove the Vowels from the string.
  6. Find All Non-Overlapping strings/characters using regular expressions .
  7. Check whether the given string is Panagram or not.
  8. Replacing strings using Regular Expression Regex.
  9. Check whether the given number is Strong number or not.
  10. Find the numbers divisible by a input numbers within the given range.
  11. Change the behaviour of the Regular Expression using flags.
  12. Find the exponentiation of a number.
  13. Find all prime numbers in a range using Sieve of Eratosthenes.
  14. Convert decimal equivalent into binary, hexadecimal and octal.
  15. Compute the area of a farmers field in feet.
  16. Compute time from number of days/hours to seconds.
  17. Precompiled patterns using Regular Expressions .
  18. Compute the price of the cake on the nth day.
  19. Compute the number of possible non-increasing arrays.
  20. Find the type of spinach using the calorie value.