regex - python raw string escaping character -


a noob question. reading documentation on use of regexes in python. under impression using raw string treat '\' , not consider whatever following escape sequence. in example reading, however,

>>> phonenumregex = re.compile(r'(\(\d\d\d\)) (\d\d\d-\d\d\d\d)') >>> mo = phonenumregex.search('my phone number (415) 555-4242.') >>> mo.group(1) '(415)' >>> mo.group(2) '555-4242' 

clearly, author has escaped '(' '\' . want understand how. thought putting 'r' @ bring treat '\' no differently.

the \ in raw string literal literal \, need use escape shorthand character classes , special regex characters with.

the ( beginning of grouping construct , there must closing unescaped ). these (...) never part of match. \( , \) literal ( , ) , these part of match.

think of regex engine customer delivered string. re requires \d. when use "\d", python thinks escape sequence \n, not, retains \ since default behavior unknown escape sequence , gives \d re engine. when write r"\d" python knows \ literal \ , readily provide \d re engine.


Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -