Jun 10, 2009

EReg

First obstacle, I couldn’t figure out how to loop through the results of a regular expression by looking on the haXe website, or googling for it, so I thought I’d post it here (found it in the must-have haXe & neko book):

var r = ~/<%(.*?)%>/sm;
var s = file;
while( r.match( s ) ) {
    trace( r.matched( 1 ) );
    s = r.matchedRight();
}

It’s an example from the ehx project. It extracts all parts within tags. The plan is to compile those parts using hscript.

I can’t help to think that EReg could’ve extended Iterable or something like that so I could do something like this instead:

var r = ~/<%(.*?)%>/sm;
for( m in r.match( file ) )
    trace( m.group( 1 ) );

Which, with the new “using” feature in haXe 2.04 could be (assuming there’s some kind of ERegTools.match( str , ereg );):

for( m in file.match( ~/<%(.*?)%>/sm ) )
    trace( m.group( 1 ) );

But I guess that since this would have to return some kind of Match object every time it would probably be slower than keeping the references inside the EReg instance. Or it might be an issue with some targets. Maybe it’s something I might add to the “current projects” list.

Comment
About
developing software is fun.
sitting in a pleasant sofa is fun.
combine for double the fun.
Search