[racket] Regexp question

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Tue Apr 15 15:39:24 EDT 2014

If you need a literal ] you can use (regexp-quote "]")

> (count-substring (regexp-quote "]") "a[rts[a3]]]]")
4

-- 
Jens Axel Søgaard



2014-04-15 21:09 GMT+02:00 Manfred Lotz <manfred.lotz at arcor.de>:
> On Rosetta code in "Count occurrences of a substring" I found this for
> Racket:
>
> (define count-substring
>   (compose length regexp-match*))
>
>
> Example:
> (count-substring "th" "the three truths")
> 3
>
> I like to use it for getting the number of occurences of a
> list of characters in a string.
>
> For this I used it like this:
>
>   (count-subsring "[a-z2;]" "arst;452")
>
>
> Let us assume the part between the brackets could be anything. How
> would I escape certain characters in order to make sure the regular
> expression still works fine?
>
> Examples:
>
> -> (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.
>
>
>
> --
> Manfred
>
>
>
>
>
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users



-- 
--
Jens Axel Søgaard


Posted on the users mailing list.