<div class="gmail_quote">On Sat, Jun 4, 2011 at 00:24, Matthew Flatt <span dir="ltr"><<a href="mailto:mflatt@cs.utah.edu">mflatt@cs.utah.edu</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div class="im">At Sat, 4 Jun 2011 00:09:40 -0300, Rodolfo Carvalho wrote:<br>
> Eli says that<br>
><br>
> (BTW, Racket's solution is something that is done in many other<br>
> > languages too.)<br>
><br>
><br>
><br>
> I come from Python where I can write<br>
><br>
> >>> re.findall("\d{2}", "06/03/2011")<br>
> ['06', '03', '20', '11']<br>
><br>
> And printing the string that I used for my regexp gives:<br>
><br>
> >>> print "\d{2}"<br>
> \d{2}<br>
<br>
</div>Isn't that only because "\d" isn't an escape in strings? While Racket<br>
complains about a "\" that doesn't form an escape sequence, Python<br>
treats the "\" as a literal (while Ruby effectively ignores the "\").<br>
<br>
Compare to the Python example<br>
<br>
>>> re.findall("a\b", "a ")<br>
[]<br>
>>> re.findall("a\\b", "a ")<br>
['a']<br>
<br>
Since "\b" is an escape that means ASCII 8, to get a backslash followed<br>
by a "b" in a regexp (to indicate a word boundary), you need to use<br>
"\\b".<br>
<br></blockquote><div><br></div><div><br></div><div>Yeah... thinking like this makes Python feel a bit more complex to reason about.</div><div>"\d" doesn't mean anything in the "string-world" while "\b" does, and therefore needs to have the backslash escaped.</div>
<div><br></div><div><br></div><div>Wait wait!</div><div>However, Python strings have a special raw-mode (raw-strings), suitable for writing regexps...</div><div><br></div><div><br></div><div>So I've been "lying" all the time. I was not used to write "\d{2}", but actually the raw-string r"\d{2}".</div>
<div>Using a raw-string makes Matthew's examples work as follows:</div><div><br></div><div><br></div><div><div>>>> re.findall(r"a\b", "a ")</div><div>['a']</div><div><br></div><div>
>>> re.findall(r"a\\b", "a ")</div><div>[]</div></div><div><br></div><div> </div></div><br>