[racket] Some design "whys" of regexps in Racket
At Sat, 4 Jun 2011 00:09:40 -0300, Rodolfo Carvalho wrote:
> Eli says that
>
> (BTW, Racket's solution is something that is done in many other
> > languages too.)
>
>
>
> I come from Python where I can write
>
> >>> re.findall("\d{2}", "06/03/2011")
> ['06', '03', '20', '11']
>
> And printing the string that I used for my regexp gives:
>
> >>> print "\d{2}"
> \d{2}
Isn't that only because "\d" isn't an escape in strings? While Racket
complains about a "\" that doesn't form an escape sequence, Python
treats the "\" as a literal (while Ruby effectively ignores the "\").
Compare to the Python example
>>> re.findall("a\b", "a ")
[]
>>> re.findall("a\\b", "a ")
['a']
Since "\b" is an escape that means ASCII 8, to get a backslash followed
by a "b" in a regexp (to indicate a word boundary), you need to use
"\\b".