[racket] Backreference question (regex)

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Fri Oct 29 17:39:25 EDT 2010

You're using a string as a regular expression.  They automatically get
interpreted as egrep-compatible regular expressions.  Construct a Perl
regular expression explicitly with the pregexp function or the #px
notation.  Furthermore, you need to escape your backslash; right now
it is being interpreted as a string escape and not getting as far as
the regular expression.  Try the following:

Welcome to Racket v5.0.
> (regexp-match #px"(1)\\1" "112")
'("11" "1")
>

Carl Eastlund

On Fri, Oct 29, 2010 at 5:36 PM, Justin Phillips
<jjustinphillipss at gmail.com> wrote:
> I have been using regexp-match
> specifically --
> (regexp-match "(1)\1" "112")
> I'm 100% positive I'm missing some dorky character that will make this work.
> -Justin
>
>
> On Fri, Oct 29, 2010 at 2:33 PM, Carl Eastlund <cce at ccs.neu.edu> wrote:
>>
>> I'm not sure I know what problem you're running into, but if you use
>> Perl regular expression be sure to use (pregexp ...) to compile them
>> or #px"..." to write them.  Otherwise you get a slightly different
>> regular expression language (compatible with Unix utilities like
>> egrep, rather than with Perl).  If you're using those regular
>> expressions, they don't have backreferences.
>>
>> Carl Eastlund
>>
>> On Fri, Oct 29, 2010 at 5:29 PM, Justin Phillips
>> <jjustinphillipss at gmail.com> wrote:
>> > I'm trying to write a small regex that would return any repeating
>> > pattern in
>> > a string e.g. ("ababcdcdababcdcd" would return "ab" "cd" abab" "cdcd"
>> > "ababcdcd")
>> > I'm having trouble using the backreferencing. I see under section 3.7.2
>> > of
>> > the guide (and also some Perl docs I've checked out) that I should be
>> > able
>> > to write a simple regex "(1)\1" which would return true for the data
>> > "113"
>> > but Racket says I'm wrong. Any help would be greatly appreciated.
>> > -Justin


Posted on the users mailing list.