www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What's the right way to test if regex.match() hits or not?

reply Russell Lewis <webmaster villagersonline.com> writes:
I'm trying to use regex.match() for a tokenizer.  So I want to compare 
my input to a bunch of different regex'es, expecting one to hit and the 
rest not to.

Problem is, I can't seem to figure out the "right" way to ask if a hit 
occurred.  My code is roughly this:

BEGIN CODE
while(input.length > 0)
{
	auto r1 = regex("^...something nasty...");
	auto r2 = regex("^...something worse...");

	auto tmp = match(input, r1);
	if(<check r1 here>)
	{
		ReportTokenType1(tmp.hit())
		input = tmp.post();
		continue;
	}

	... and so on ...
}
END CODE

What do I put inside the if()?

P.S. Does anybody know why dmd complains "cannot evaluate at compile 
time" when I set those regex objects to "static invariant" so I'm not 
rebuilding them with every pass?
Jun 01 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Jun 2, 2009 at 12:16 AM, Russell Lewis
<webmaster villagersonline.com> wrote:

 P.S. Does anybody know why dmd complains "cannot evaluate at compile time"
 when I set those regex objects to "static invariant" so I'm not rebuilding
 them with every pass?
Because it wants you to have a constant expression to initialize a static variable, and so it tries to evaluate regex(...) at compile time. You know, there's a much easier way to prevent them from being reinitialized every iteration - move their declaration above the loop.
Jun 02 2009
parent reply Russell Lewis <webmaster villagersonline.com> writes:
Jarrett Billingsley wrote:
 On Tue, Jun 2, 2009 at 12:16 AM, Russell Lewis
 <webmaster villagersonline.com> wrote:
 
 P.S. Does anybody know why dmd complains "cannot evaluate at compile time"
 when I set those regex objects to "static invariant" so I'm not rebuilding
 them with every pass?
Because it wants you to have a constant expression to initialize a static variable, and so it tries to evaluate regex(...) at compile time. You know, there's a much easier way to prevent them from being reinitialized every iteration - move their declaration above the loop.
Of course! But then, the code is harder to read. I'll bite the bullet and move it above the loop, but it's ugly. And yeah, I know about CTFI and the issues there...I just thought that regex was designed to allow CTFI. I guess I was wrong? BTW: I found out how to check if there was a match or not: the empty() function.
Jun 02 2009
parent reply Jarrett Billingsley <jarrett.billingsley gmail.com> writes:
On Tue, Jun 2, 2009 at 11:47 AM, Russell Lewis
<webmaster villagersonline.com> wrote:

 Of course! =A0But then, the code is harder to read. =A0I'll bite the bull=
et and
 move it above the loop, but it's ugly.

 And yeah, I know about CTFI and the issues there...I just thought that re=
gex
 was designed to allow CTFI. =A0I guess I was wrong?
For future reference, it's "CTFE", and I wasn't aware that regex was designed for it.
Jun 02 2009
parent Russell Lewis <webmaster villagersonline.com> writes:
Jarrett Billingsley wrote:
 On Tue, Jun 2, 2009 at 11:47 AM, Russell Lewis
 <webmaster villagersonline.com> wrote:
 
 Of course!  But then, the code is harder to read.  I'll bite the bullet and
 move it above the loop, but it's ugly.

 And yeah, I know about CTFI and the issues there...I just thought that regex
 was designed to allow CTFI.  I guess I was wrong?
For future reference, it's "CTFE", and I wasn't aware that regex was designed for it.
I kept looking at that acronym, and it just didn't look right...thanks for the correction. :)
Jun 02 2009