0

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]')
  1. What is the meaning of r?
  2. What is the meaning of \', \?, \, and \.?
  3. What is the meaning of \t\r\n ?
Christian Dean
  • 22,138
  • 7
  • 54
  • 87
Starz
  • 199
  • 1
  • 6
  • 3
    I don't mean to be rude, but your questions have nothing to do with `re.compile()`, so its no wonder the docs you looked at didn't help. You were looking in the wrong place. You are asking about [string prefixes](https://docs.python.org/3.5/reference/lexical_analysis.html#string-and-bytes-literals) and [escape characters](https://docs.python.org/3.5/reference/lexical_analysis.html#string-and-bytes-literals). – Christian Dean Jun 22 '17 at 03:57
  • Ah yes.. sorry.. editing the title.. – Starz Jun 22 '17 at 03:58
  • explained in the doc https://docs.python.org/2/library/re.html – lkdhruw Jun 22 '17 at 04:00
  • Please further edit as appropriate. thank you! – Starz Jun 22 '17 at 04:00
  • r is raw format. \' \+ etc is going to treat them as the same characters and nothing special. \t tab \r carriage return \n new line characters – anugrah Jun 22 '17 at 04:06
  • Thank you everyone! – Starz Jun 22 '17 at 04:23

1 Answers1

1

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.

foslock
  • 3,639
  • 2
  • 22
  • 26