[racket] Regexp question
On Tue, Apr 15, 2014 at 09:09:00PM +0200, Manfred Lotz wrote:
> -> (count-substring "[\\[]" "a[rts[a3]")
> 2
> -> (count-substring "[a-z\\[]" "a[rts[a3]")
> 7
> -> (count-substring "[\\]]" "a[rts[a3]")
> 0
>
> The first two seem to work fine, the last one doesn't.
Those backslashes are not escaping the brackets, they are part of the
bracketed expressions.
> (count-substring "[\\[]" "\\[")
2
In your third example above, the regex specifies a pattern consisting of
a backslash (bracketed by itself) followed by a closing square bracket.
That pattern is not matched above, but here it is:
> (count-substring "[\\]]" "\\]")
1
To use a ] in a bracketed expression, place it first. To use a -, place
it first or last. A [ in a bracketed expression can go anywhere:
> (count-substring "[][-]" "-[]")
3
David