Character Classes
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 |
Examples:
Python3
import re print (re.search(r "\s" , "xenon is a gas" )) print (re.search(r "\D+\d*" , "123geeks123" )) |
Output:
<re.Match object; span=(5, 6), match=' '>
<re.Match object; span=(3, 11), match='geeks123'>
Explanation:
In the first example, \s will search for the blank space and whenever it encounters the first blank space it will print out that match. Since xenon is a gas that contains blank spaces, it will encounter the first blank space and print out that match(‘ ‘) and its position(5,6)
In the second example, \D+\d* will search for one or more non-digits characters followed by 0 or more digits. In our case, geeks123 best fits the description as it contains 1 or more non-digits characters(geeks) followed by 0 or more digit characters(123). So it will print the match(‘geeks123’) and its position(3,11).
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