Template regexes, version 2

Don Clugston dac at nospam.com.au
Wed Feb 22 05:30:36 PST 2006


Walter Bright wrote:
> "Don Clugston" <dac at nospam.com.au> wrote in message 
> news:dtev42$1g98$1 at digitaldaemon.com...
>> Perl-ish languages deal with the last case by allowing embedded variables 
>> in strings. The template regexes Ive been developing can cope with them by 
>> using additional parameters and extended syntax, eg @1 for the string 
>> value of the first parameter.
>> eg
>>
>> char [] var1 = "here";
>> char [] input = " a56aherea";
>> char [] x = firstmatch!("a at 1a")(input, var1);
>> var1 = "56";
>> char [] y = firstmatch!("a at 1a")(input, var1);
>>
>> would give x == "aherea", y=="aherea".
> 
> Great! But can I suggest using the format used by 
> std.regexp.RegExp.replace() ? It uses $$, $&, $`, $', and $n.

OK. Except ... I think replace() doesn't have to worry about $ colliding 
with the end of line marker? Anyway, I've used $n for now.
(Doesn't SQL use @ for parameters in stored procedures? Can't quite 
remember.)

>> How good is DMD at performing this optimisation?
> 
> Not good enough. But I wouldn't worry about that for the moment.

I'm not really surprised -- I imagine the back-end doesn't have much 
that's specific to D.

I've attached what I've done. It's not complete -- doesn't do ranges, or 
any of the escape characters, or beginning and end of line.
But it does do the full set of repetition operators, parentheses,
and unions (arbitrarily nestable). It also does the $n parameters.
Most of the remaining stuff is straightforward, and not very exciting.

I feel as though I've finally worked out what mixins are for. They are a 
perfect match for this application.

In any case, this should give you a good idea for what you'll able to 
use in your presentation.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: regexp2.d
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20060222/f4979a63/attachment.ksh>


More information about the Digitalmars-d mailing list