Flags

Expression Explanation

a

Matches ASCII only

Ignore case

L

Locale character classes

m

^ and $ match start and end of the line (Multi-line)

s

Matches everything including newline as well

u

 Matches Unicode character classes

x

Allow spaces and comments (Verbose)

Examples:

Python3




import re
  
exp = """hello there
I am from
Geeks for Geeks"""
  
print(re.search(r"and", "Sun And Moon", flags=re.IGNORECASE))
print(re.findall(r"^\w", exp, flags = re.MULTILINE))


Output:

<re.Match object; span=(4, 7), match='And'>
['h', 'I', 'G']

Explanation:

In the first example, the IGNORECASE flag will search for the word and irrespective of its case (whether uppercase or lowercase), So it ignores the case and matches And in the expression. So it will print the match(‘And’) and its position(4,7)

In the second example, the MULTILINE flag will search in each and every line and will match when the line is starting an alphanumeric character. Since in Multi-line hello there I am from Geeks for Geeks, every line is starting with an alphanumeric character, so it will match every line and will print the match in an array ([‘h’, ‘I’, ‘G’]).

Note: In MULTILINE flag, we have to use re.findall, since it has many matches (for every line)

Regex Cheat Sheet – Python

Regex or Regular Expressions are an important part of Python Programming or any other Programming Language. It is used for searching and even replacing the specified text pattern. In the regular expression, a set of characters together form the search pattern. It is also known as the reg-ex pattern. The tough thing about Regex is not learning or understanding it but remembering syntax and how to form patterns according to our requirements.

So, here we have provided a Regex Cheat Sheet containing all the different character classes, special characters, modifiers, sets, etc. which are used in the regular expression.

Table of Content

  • Basic Characters
  • Quantifiers
  • Character Classes
  • Sets
  • Groups
  • Assertions
  • Flags

Similar Reads

Basic Characters:

Expression  Explanations ^ Matches the expression to its right, at the start of a string before it experiences a line break $ Matches the expression to its left, at the end of a string before it experiences a line break . Matches any character except newline a Matches exactly one character a xy Matches the string xy a|b Matches expression a or b. If a is matched first, b is left untried....

Quantifiers:

...

Character Classes:

Expressions Explanations + Matches the expression to its left 1 or more times. * Matches the expression to its left 0 or more times. ? Matches the expression to its left 0 or 1 times {p} Matches the expression to its left p times, and not less. {p, q} Matches the expression to its left p to q times, and not less. {p, } Matches the expression to its left p or more times. { , q} Matches the expression to its left up to q times...

Sets:

...

Groups:

Expressions Explanations \w  Matches alphanumeric characters, that is a-z, A-Z, 0-9, and underscore(_) \W Matches non-alphanumeric characters, that is except a-z, A-Z, 0-9 and _ \d Matches digits, from 0-9. \D  Matches any non-digits. \s Matches whitespace characters, which also include the \t, \n, \r, and space characters. \S Matches non-whitespace characters. \A Matches the expression to its right at the absolute start of a string whether in single or multi-line mode. \Z  Matches the expression to its left at the absolute end of a string whether in single or multi-line mode. \n Matches a newline character \t Matches tab character \b Matches the word boundary (or empty string) at the start and end of a word. \B Matches where \b does not, that is, non-word boundary...

Assertions:

...

Flags:

Expressions Explanations [abc]  Matches either a, b, or c. It does not match abc. [a-z] Matches any alphabet from a to z. [A-Z] Matches any alphabets in capital from A to Z [a\-p] Matches a, -, or p. It matches – because \ escapes it. [-z] Matches – or z [a-z0-9] Matches characters from a to z or from 0 to 9. [(+*)]  Special characters become literal inside a set, so this matches (, +, *, or ) [^ab5]  Adding ^ excludes any character in the set. Here, it matches characters that are not a, b, or 5. \[a\] Matches [a] because both parentheses [ ] are escaped...

Conclusion

...