Python Regex – How to Escape Special Characters

Often while creating Regex patterns and parsing through strings, we will run into a rather unexpected problem while using certain “special characters” in our target pattern (the pattern we are searching for). For example, the “+” (plus operator), which normally means “one or more character” in Regex. But what if our target string contains the character “+”? In order to handle this situation in Regex, we need to escape these special characters.

Lets begin with the tutorial!


Escaping Special Characters in Regex – Example#1

To further understand this problem, lets take a look at an example where this problem occurs.

In the below code, we will attempt to find the word “A+” in our string, and print them out all matches to the console. Sounds simple enough, but it will not work!

mystring = "I got A+ in Maths. Everyone likes an A+!"
print(re.findall("A+", mystring))
['A', 'A']

As you can see here, the output is not what we expected. And that is because our Regex pattern is not searching for the word “A+”, rather it is searching for “a string with one or more ‘A’ characters”.

Lets take one a look at one more problem that can occur, before we discuss the solutions.


Example#2

Sometimes you may be using multiple special characters in a row. This can actually throw an error in some cases, such as when you combine two + operators.

The below example shows a code snippet where we search for the word “C++”.

mystring = "C++ is Amazing. Everyone likes C++!"
print(re.findall("C++", mystring))
re.error: multiple repeat at position 2

As you can see, this time we didn’t even get any output. We just got an error that crashed our program. Clearly the problem here is that Regex misinterprets our use of these characters. It is our job to make it clear to Regex whether we are using these characters (+, ?, . , ^ etc.) as regular characters or special characters.

We do this by “escaping” the characters when we want to use them as regular characters.


How to escape characters?

So how do we escape characters? Well, all we have to do is add a simple “backslash” before each special character. Lets now fix our earlier two examples by adding a backslash in the appropriate locations.

mystring = "I got A+ in Maths. Everyone likes an A+!"
print(re.findall("A\+", mystring))
['A+', 'A+']

As you can see, we are now getting the expected output.

Now for example number two!

mystring = "C++ is Amazing. Everyone likes C++!"
print(re.findall("C\+\+", mystring))
['C++', 'C++']

We hope you enjoyed the tutorial!


This marks the end of the How to Escape Special Characters in Python Regex Tutorial. Any suggestions or contributions for CodersLegacy are more than welcome. Questions regarding the tutorial content can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments