http://qs321.pair.com?node_id=812993


in reply to Re^2: Regex fun
in thread Regex fun

I would like single regex as, as was pointed out, the double regex is wasteful since the matching has to be done twice.

The 2-regex version that I proposed avoids a lot of the double matching (it converts 2 number searches into 1 number search and then a hunt for a literal string). However, only benchmarking (which I'm too lazy to do) will show whether it's actually faster.

If s/// set pos (and behaved like m// in a while loop), then one could avoid any doubled effort at all:

s/\G\+$1[$bases]{$1}// while s/\+([0-9]+)//g;
(UPDATE: but note that this is fanciful, non-working code). In fact, that's what I had originally, until I tested it and discovered that it didn't work.

Running yours left strange things in it (+-1 and +0 below).

Oops, sorry! My original regex wasn't smart enough to stop grabbing bases once the count indicated that there were no more needed. I've fixed it, but, since the only point was its brevity (it's frightfully inefficient), it's not much fun any more.

I think adding /c in the match pattern should fix the problem with my while and possibly the (??{...}) version as well?

I'm not sure what you mean. I am pretty sure (but can't find the documentation) that /c only has an effect on the semantics of failed matches, and those aren't our problem here. Notice that (??{ }) doesn't have a problem to be fixed—the application you have in mind is essentially exactly for what that escape was designed (I assume), and it doesn't require any extra trickery.