digitalmars.D.learn - What's the right way to test if regex.match() hits or not?
- Russell Lewis (24/24) Jun 01 2009 I'm trying to use regex.match() for a tokenizer. So I want to compare
- Jarrett Billingsley (6/9) Jun 02 2009 Because it wants you to have a constant expression to initialize a
- Russell Lewis (7/19) Jun 02 2009 Of course! But then, the code is harder to read. I'll bite the bullet
- Jarrett Billingsley (6/10) Jun 02 2009 et and
- Russell Lewis (3/14) Jun 02 2009 I kept looking at that acronym, and it just didn't look right...thanks
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
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
Jarrett Billingsley wrote:On Tue, Jun 2, 2009 at 12:16 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? BTW: I found out how to check if there was a match or not: the empty() function.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
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 andmove it above the loop, but it's ugly. And yeah, I know about CTFI and the issues there...I just thought that re=gexwas 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
Jarrett Billingsley wrote:On Tue, Jun 2, 2009 at 11:47 AM, Russell Lewis <webmaster villagersonline.com> wrote:I kept looking at that acronym, and it just didn't look right...thanks for the correction. :)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.
Jun 02 2009