[racket] Regexp question

From: David T. Pierson (dtp at mindstory.com)
Date: Tue Apr 15 23:09:46 EDT 2014

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

Posted on the users mailing list.