Restrictions in std.regexp?

Derek Parnell derek at psych.ward
Tue May 2 07:08:55 PDT 2006


On Tue, 02 May 2006 23:39:13 +1000, Olaf Pohlmann <op at nospam.org> wrote:

> Hi,
>
> the documentation of std.regexp is somewhat sparse, so I tried to find  
> out a few things on my own. There seems to be no way to do lookaheads  
> and lookbehinds. This:
>
> 	RegExp re = search("ABCDEF", "(?<=AB)CD(?=EF)");
>
> should find "CD" as a match, but it yields a runtime error:
>
> 	Error: *+? not allowed in atom
>
> Is there any other way to get this working or am I just out of luck with  
> the current implementation?

I can't tell what it is you are trying to do but it seems that the RE  
syntax you are expecting is not what has been implemented. See  
http:http://www.digitalmars.com/ctg/regular.html for details.

Are you looking for an optional "AB" followed by "CD" followed by an  
optional "EF" ?

If so try

     RegExp re = search("ABCDEF", "(AB)?(CD)(EF)?");

Here is a sample program ...

import std.stdio;
import std.regexp;

void main()
{
   RegExp re = search("AXCDEFGHI", "(AB)?(CD)(EF)?");

   writefln("PRE: %s", re.pre());
   writefln("MATCH: %s", re.match(0));
   writefln("SUB1: %s", re.match(1));
   writefln("SUB2: %s", re.match(2));  // this should be 'CD'
   writefln("SUB3: %s", re.match(3));
   writefln("POST: %s", re.post());
}

-- 
Derek Parnell
Melbourne, Australia



More information about the Digitalmars-d-learn mailing list