![]() |
|
[Tut] Python Re Escape - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python Re Escape (/thread-93800.html) |
[Tut] Python Re Escape - xSicKxBot - 03-01-2020 Python Re Escape I don’t know how often I sat in front of my computer, writing regular expressions and wondering: how to escape this or that character? The problem is that some special characters have a special meaning in Python strings and regular expressions. If you want to remove the special meaning, you need to escape the characters with an additional backslash. If you have this problem too, you’re in luck. This article is the ultimate guide to escape special characters in Python. Just click on the topic that interests you and learn how to escape the special character you’re currently struggling with! If you’re the impatient guy, you’re in luck too. Just try to add the backslash to your special character you want to escape: Code: \xCode: xHere are a few examples: Code: >>> import re
>>> re.findall('\( \{ \" \. \* \+', r'( { " . * +')
['( { " . * +']You can also watch the following video where I give you a quick example: Python Regex Escape Characters If you use special characters in strings, they carry a special meaning. Sometimes you don’t need that. The general idea is to escape the special character Code: xCode: \xIn the following, I show how to escape all possible special characters for Python strings and regular expressions: Python Regex Escape Parentheses () How to escape the parentheses Code: (Code: )Parentheses have a special meaning in Python regular expressions: they open and close matching groups. You can get rid of the special meaning of parentheses by using the backslash prefix: Code: \(Code: \)Code: >>> import re
>>> re.findall(r'\(.*\)', 'Python is (really) great')
['(really)']The result shows a string that contains the “special” characters Code: '('Code: ')'Python Regex Escape Square Brackets [] How to escape the square brackets Code: [Code: ]Square brackets have a special meaning in Python regular expressions: they open and close character sets. You can get rid of the special meaning of brackets by using the backslash prefix: Code: \[Code: \]Code: >>> import re
>>> re.findall(r'\[.*\]', 'Is Python [really] easy?')
['[really]']The result shows a string that contains the “special” characters Code: '['Code: ']'Python Regex Escape Curly Brace (Brackets) How to escape the curly braces Code: {Code: }The curly braces don’t have any special meaning in Python strings or regular expressions. Therefore, you don’t need to escape them with a leading backslash character Code: \Code: >>> import re
>>> re.findall(r'\{.*\}', 'if (2==2) { y = 3; }')
['{ y = 3; }']
>>> re.findall(r'{.*}', 'if (2==2) { y = 3; }')
['{ y = 3; }']
>>> re.findall('{.*}', 'if (2==2) { y = 3; }')
['{ y = 3; }']All three cases match the same string enclosed in curly braces—even though we did not escape them and didn’t use the raw string Code: r''Python Regex Escape Slash (Backslash and Forward-Slash) How to escape the slash characters—backslash Code: \Code: /The backslash has a special meaning in Python regular expressions: it escapes special characters and, thus, removes the special meaning. (How meta.) Code: >>> import re
>>> re.findall(r'\\...', r'C:\home\usr\dir\hello\world')
['\\hom', '\\usr', '\\dir', '\\hel', '\\wor']You can see that the resulting matches have escaped backslashes themselves. This is because the backslash character has a special meaning in normal strings. Thus, the Python interpreter escapes it automatically by itself when printing it on the shell. Note that you didn’t need to escape the backslash characters when writing the raw string Code: r'C:\home\usr\dir\hello\world'Code: >>> re.findall(r'\\...', 'C:\\home\\usr\\dir\\hello\\world')
['\\hom', '\\usr', '\\dir', '\\hel', '\\wor']In contrast to the backslash, the forward-slash doesn’t need to be escaped. Why? Because it doesn’t have a special meaning in Python strings and regular expressions. You can see this in the following example: Code: >>> import re
>>> re.findall('/...', '/home/usr/dir/hello/world')
['/hom', '/usr', '/dir', '/hel', '/wor']The result shows that even in a non-raw string, you can use the forward-slash without leading escape character. Python Regex Escape String Single Quotes How to escape the single quotes Code: 'Single quotes have a special meaning in Python regular expressions: they open and close strings. You can get rid of the special meaning of single quotes by using the backslash prefix: Code: \'Code: >>> import re
>>> re.findall('\'.*\'', "hello 'world'")
["'world'"]The result shows a string that contains the “special” single quote characters. The result also shows an alternative that removes the special meaning of the single quotes: enclose them in double quotes: Code: "hello 'world'"Python Regex Escape String Double Quotes How to escape the double quotes Code: "Double quotes have a special meaning in Python regular expressions: they open and close strings. You can get rid of the special meaning of single quotes by using the backslash prefix: Code: \"Code: >>> import re
>>> re.findall('\".*\"', 'hello "world"')
['"world"']The result shows a string that contains the “special” single quote characters. The result also shows an alternative that removes the special meaning of the single quotes: enclose them in double quotes: Code: 'hello "world"'Python Regex Escape Dot (Period) How to escape the regex dot (or period) meta character Code: .The dot character has a special meaning in Python regular expressions: it matches an arbitrary character (except newline). You can get rid of the special meaning of the dot character by using the backslash prefix: Code: \.Code: >>> import re
>>> re.findall('..\.', 'my. name. is. python.')
['my.', 'me.', 'is.', 'on.']The result shows four strings that contain the “special” characters Code: '.'Python Regex Escape Plus How to escape the plus symbol Code: +The plus symbol has a special meaning in Python regular expressions: it’s the one-or-more quantifier of the preceding regex. You can get rid of the special meaning of the regex plus symbol by using the backslash prefix: Code: \+Code: >>> import re
>>> re.findall('\++', '+++python+++rocks')
['+++', '+++']The result shows both usages: the plus symbol with and without leading escape character. If it is escaped Code: \+Code: +Python Regex Escape Asterisk How to escape the asterisk symbol Code: *The asterisk symbol has a special meaning in Python regular expressions: it’s the zero-or-more quantifier of the preceding regex. You can get rid of the special meaning of the regex asterisk symbol by using the backslash prefix: Code: \*Code: >>> import re
>>> re.findall('\**', '***python***rocks')
['***', '***']The result shows both usages: the asterisk symbol with and without leading escape character. If it is escaped Code: \*Code: *Python Regex Escape Question Mark How to escape the question mark symbol Code: ?The question mark symbol has a special meaning in Python regular expressions: it’s the zero-or-one quantifier of the preceding regex. You can get rid of the special meaning of the question mark symbol by using the backslash prefix: Code: \?Code: >>> import re
>>> re.findall('...\?', 'how are you?')
['you?']The result shows that the question mark symbol was matched in the given string. Python Regex Escape Underscore How to escape the underscore character Code: _The underscoredoesn’t have a special meaning in Python regular expressions or Python strings. Therefore, you don’t need to escape the underscore character—just use it in your regular expression unescaped. Code: >>> import re
>>> re.findall('..._', 'i_use_underscore_not_whitespace')
['use_', 'ore_', 'not_']However, it doesn’t harm to escape it either: Code: >>> re.findall('...\_', 'i_use_underscore_not_whitespace')
['use_', 'ore_', 'not_']In both cases, Python finds the underscore characters in the string and matches them in the result. Python Regex Escape Pipe How to escape the pipe symbol Code: |The pipe symbol has a special meaning in Python regular expressions: theregex OR operator. You can get rid of the special meaning of the pipe symbol by using the backslash prefix: Code: \|Code: >>> import re
>>> re.findall('.\|.', 'a|b|c|d|e')
['a|b', 'c|d']By escaping the pipe symbol, you get rid of the special meaning. The result is just the matched pipe symbol with leading and trailing arbitrary character. If you don’t escape the pipe symbol, the result will be quite different: Code: >>> re.findall('.|.', 'a|b|c|d|e')
['a', '|', 'b', '|', 'c', '|', 'd', '|', 'e']In this case, the regex Code: .|.Python Regex Escape Dollar How to escape the dollar symbol Code: $The dollar symbol has a special meaning in Python regular expressions: it matches at the end of the string. You can get rid of the special meaning by using the backslash prefix: Code: \$Code: >>> import re
>>> re.findall('\$\d+', 'Your house is worth $1000000')
['$1000000']Note that the Code: \d+Python Regex Escape Greater Than and Smaller Than How to escape the greater than Code: <Code: >Greater and smaller than symbols don’t have a special meaning in Python regular expressions. Therefore, you don’t need to escape them. Here’s an example: Code: >>> import re
>>> re.findall('<.*>.*<.*>', '<div>hello world</div>')
['<div>hello world</div>']The result shows a string that even without escaping the HTML tag symbols, the regex matches the whole string. Python Regex Escape Hyphen How to escape the hyphen Code: -Outside a character set, the hyphen doesn’t have a special meaning and you don’t need to escape it. Here’s an example: Code: >>> import re
>>> re.findall('..-', 'this is-me')
['is-']The unescaped hyphen character in the regex matches the hyphen in the string. However, inside a character set, the hyphen stands for the range symbol (e.g. Code: [0-9]Code: >>> re.findall('[a-z\-]+', 'hello-world is one word')
['hello-world', 'is', 'one', 'word']Note that, in this case, if you don’t escape the hyphen in the character set, you get the same result: Code: >>> re.findall('[a-z-]+', 'hello-world is one word')
['hello-world', 'is', 'one', 'word']The reason is that the hyphen appears at the end of the character set where it can have only one meaning: the hyphen symbol itself. However, in all other cases, the hyphen would be assumed to mean the range character which will result in strange behavior. A good practice is, thus, to escape the hyphen in the character class per default. Python Regex Escape Newline In a recent StackOverflow article, I read the following question: I got a little confused about Python raw string. I know that if we use raw string, then it will treat Code: '\'Code: r'\n'Code: '\'Code: 'n'Code: r'\n'The coder asking the question has understood that the Python interpreter doesn’t assume that the two characters Code: \Code: nHowever, those two symbols have a special meaning for the regex engine! So if you use them as a regular expression pattern, they will indeed match the newline character: Code: >>> import re
>>> text = '''This
is
a
multiline
string'''
>>> re.findall(r'[a-z]+\n', text)
['his\n', 'is\n', 'a\n', 'multiline\n']Therefore, you don’t need to escape the newline character again to match it in a given string. Python re.escape Method If you know that your string has a lot of special characters, you can also use the convenience method Code: re.escape(pattern)Specification: Code: re.escape(pattern)Definition: escapes all special regex meta characters in the given Code: patternExample: you can escape all special symbols in one go: Code: >>> re.escape('https://www.finxter.com/') 'https://www\\.finxter\\.com/'The dot symbol has a special meaning in the string Code: 'https://www.finxter.com/'Note that “only characters that can have special meaning in a regular expression are escaped. As a result, Code: '!'Code: '"'Code: '%'Code: "'"Code: ','Code: '/'Code: ':'Code: ';'Code: '<'Code: '='Code: '>'Code: '@'Code: "`"Python Regex Bad Escape There are some common errors in relation to escaping in Python regular expressions. If you try to escape a normal character that has not a special meaning, Python will throw a “bad escape error”: Code: >>> re.findall('\m', 'hello {world}')
Traceback (most recent call last): File "<pyshell#61>", line 1, in <module> re.findall('\m', 'hello {world}') File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\re.py", line 223, in findall return _compile(pattern, flags).findall(string) File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\re.py", line 286, in _compile p = sre_compile.compile(pattern, flags) File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\sre_compile.py", line 764, in compile p = sre_parse.parse(p, flags) File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\sre_parse.py", line 930, in parse p = _parse_sub(source, pattern, flags & SRE_FLAG_VERBOSE, 0) File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\sre_parse.py", line 426, in _parse_sub not nested and not items)) File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\sre_parse.py", line 507, in _parse code = _escape(source, this, state) File "C:\Users\xcent\AppData\Local\Programs\Python\Python37\lib\sre_parse.py", line 402, in _escape raise source.error("bad escape %s" % escape, len(escape))
re.error: bad escape \m at position 0As the error message suggests, there’s no escape sequence Code: \mWhere to Go From Here Wow, you either have read about a lot of escaped character sequences or you did a lot of scrolling to reach this point. In both cases, you have a great advantage over other coders: you’re a persistent guy or gal! Do you want to increase your advantage over your peers? Then join my Python email academy! I’ll teach you the ins and outs of Python coding—all free! Join Finxter Email Academy, become a better coder, and download your free Python cheat sheets! |