[racket] Regexp question

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Tue Apr 15 15:09:00 EDT 2014

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






Posted on the users mailing list.