From: ml.morus.walter@... (Morus Walter) Date: 2006-10-24T13:40:10+09:00 Subject: Re: DRY fanatics? In article , "Ken Bloom" writes: > 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. A couple tips > though: > > * Use .+? instead of [^']+ > .+? does a non-greedy match, which is what you're really trying to say > with the [^']+ > Really? What about input like "bla, 'bl'ub', 'foo', 'bar'" You'll easily see that the non-greedy version matches, whereas the original regex doesn't. You have to be *very* careful if you use non-greedy matches instead of explicit exclusion, when the match is followed by further rules. /'[^']+'/ and /'.+?'/ are equivalent, but /'[^']+',/ and /'.+?',/ are not. My rule of thumb is to avoid non greedy matches in complex regexes. M.