Reputation: 199
Need help in interpreting string prefixes and escape character. I found this when I was learning about the arguments of re.compile()
commands below.
a = re.compile(r'^([a-z]|_)*$')
b = re.compile(r'^([a-z]|_)*:([a-z]|_)*$')
c = re.compile(r'[=\+/&<>;\'"\?%#$@\,\. \t\r\n]')
r
?\'
, \?
, \,
and \.
?\t\r\n
?Upvotes: 0
Views: 540
Reputation: 3939
What is the meaning of r?
This is the raw prefix for a string literal. Essentially it prevents normal escaping from occurring, leaving in backslashes. A more in depth explanation was given here: https://stackoverflow.com/a/2081708/416500
What is the meaning of \', \?, \, and .?
These are regex specific characters that are escaped by the backslashes. The \?
tells it to look for a literal ?
, the \,
tells it to look for a literal ,
and the \.
tells it to look for a literal .
.
What is the meaning of \t\r\n ?
\t
is the tab character, \r
is a carriage return, and \n
is the newline character. These all render as whitespace in most programs, but are stored differently.
A handy tool for breaking down regex patterns I use a lot is Regex Pal (no affiliation) which lets you hover over parts of the regex to see how it is compiled.
Upvotes: 1