On 10/21/06, Ken Bloom <kbloom / gmail.com> wrote:
> On Sun, 22 Oct 2006 10:02:37 +0900, Giles Bowkett wrote:
>
> > Anybody know a way to make this DRYer?
> >
> >     when /^([A-Za-z0-9,]+), '([^']+)', '([^']+)', '([^']+)'/
> >
> > a literal regex with a subpattern repeated three times
> >
> > I could probably split on the ', but it seems that might have unwanted
> > side effects.
> >
>
> That's fine. I see no reason to make it more obfuscated.

Well, see, I used to be a Perl guy. I'm used to thinking of
obfuscation as its own reward.

> A couple tips though:
>
>  * Use .+? instead of [^']+
>    .+? does a non-greedy match, which is what you're really trying to say
>    with the [^']+

Cheers. You're absolutely right there. I knew there was a way to do
non-greedy matching but couldn't recall it.

>  * If you want to match the *same* text three times, for example
>    "a, '1', '1', '1'" but not "a, '1', '2', '3'", then you should use a
>    backsubstitution in the match, using \2 twice, instead of the second
>    two groups, so the pattern becomes
>    /^([A-Za-z0-9,]+), '([^']+)', '\2', '\2'/

ah ok. that is a crucial difference. the data was in the same pair of
' -s each time, but it was different data each time also. so that \2
wouldn't actually have worked for me in this case.

-- 
Giles Bowkett
http://www.gilesgoatboy.org