[racket] Regexp question

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Tue Apr 15 23:47:33 EDT 2014

On Tue, 15 Apr 2014 23:09:46 -0400
"David T. Pierson" <dtp at mindstory.com> wrote:

> 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
> 

>From above it seems I have basically two options to handle arbitrary
strings to fit in a character class.


1. i have to watch out for ] and -. If found I have to put them at
first resp. last place and in the middle I have just the other
characters of my string.

2. I enclose each character with brackets and connect them with |.


Perhaps I will do 1.

-- 
Thanks, Manfred







Posted on the users mailing list.