This tutorial covers special characters in Python Regex.
Regex Special Characters are pretty useful due to how simple and powerful they are (as compared to metacharacters). A single special character can result in a powerful search query such as finding a string with no digits in it, or vice versa (finding a string with digits).
Regex Special Characters
Here is a table showing all of the available special characters in the Python Regex Library. Examples are included below the table.
Character | Description |
---|---|
\A | Checks to see whether the specified character(s) are at the start of the string. Matches the expression to its right to the start of the string. |
\b | Checks to see whether the specified character(s) are at the start or end of the string. |
\B | Checks to see whether the specified character(s) are present in the string, but not at the beginning. The inverse of \b . |
\d | Checks to see if the string has any digits in it. |
\D | Checks to see if the string doesn’t have any digits. (Inverse of \d ) |
\s | Checks to see if the string has any whitespace(s) in it. Also counts tabs (any type of whitespace counts, not just regular space) |
\S | Checks to see if there aren’t any whitespace(s) in it. (The inverse of \s ) |
\t | Checks to see if string has any “tabs” in it. |
\w | Checks for regular (alphanumeric) characters such as a - z , 0 - 9 and _ (underscore). |
\W | The opposite of the \w character. |
\Z | Checks to see whether the specified character(s) are at the end of the string. Matches the expression to its left to the end of the string. |
Special Characters Examples
Here are some examples using the various special characters from the above table. Also remember that Special characters can be used together with Meta Characters for even more powerful queries and patterns.
Here is our first example where we use the \w
special sequence. We pass to it a string of random words containing letters, numbers and special characters. The \w
character helps filter out alphanumeric characters which we commonly use in text.
mystring = "Hello ### World 123 *&^"
print(re.findall("\w+", mystring))
['Hello', 'World', '123']
\w
would return individual characters, so instead we used \w+
.
This example features the \s
character. We use it together with \w+
to find words which have a space after them.
mystring = "This String Has Whitespaces"
print(re.findall("\w+\s", mystring))
['This ', 'String ', 'Has ']
If you attempt to use \s
on a string without any spaces, it will return null.
mystring = "ThisStringHasNoWhitespaces"
print(re.findall("\s", mystring))
[]
\s
normally returns all the spaces found in the string in the form of a list. You can also use \s+
to find groups of spaces.
Here we use both the \d
and \d+
characters and observe the difference in their outputs.
mystring = "1 2 3 4 5 6 77 88 99"
print(re.findall("\d", mystring))
mystring = "1 2 3 4 5 6 77 88 99"
print(re.findall("\d+", mystring))
['1', '2', '3', '4', '5', '6', '7', '7', '8', '8', '9', '9']
['1', '2', '3', '4', '5', '6', '77', '88', '99']
You can also \d+
to find and return numbers from a string of text.
mystring = "Hello ### World 123 *&^"
print(re.findall("\d+", mystring))
['123']
Here we can use the inverse of \w
, to identify all non-alphanumeric characters from the text. This also includes spaces.
mystring = "Hello ### World 123 *&^"
print(re.findall("\W+", mystring))
[' ### ', ' ', ' *&^']
The \A
special character can be used to match a pattern with the start of the string. In the below example we check for the word “Hello” in the start of a string.
mystring = "Hello Wonderful World"
print(re.findall("\AHello", mystring))
['Hello']
Just like \A
, we can use \Z
to match patterns with the end of a string.
mystring = "Hello Wonderful World"
print(re.findall("World\Z", mystring))
['World']
This marks the end of the Python Regex Special Characters tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the article content can be asked in the comments section below.