Backslash in string literals
Regular expressions aren't part of the core Python language. Thus, there isn't a special syntax for them and therefore they are handled as any other string. As we've seen in Chapter 1, Introducing Regular Expressions, the backslash character \
is used to indicate metacharacters or special forms in regular expressions. The backslash is also used in strings to escape special characters. In other words, it has a special meaning in Python. So, if we need to use the \
character, we'll have to escape it: \\
. This will give the string literal meaning to the backslash. However, in order to match inside a regular expression, we should escape the backslashes, effectively writing four back slashes: \\\\
.
Just as an example, let's write a regular expression to match \
:
>>> pattern = re.compile("\\\\") >>> pattern.match("\\author") <_sre.SRE_Match at 0x104a88e68>
As you can see, this is tedious and difficult to understand when the pattern is long.
Python...