Is str ~ regex the root of all evil, or the leaf of all good?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Thu Feb 19 06:47:07 PST 2009


Leandro Lucarella wrote:
> Bill Baxter, el 19 de febrero a las 14:50 me escribiste:
> [snip]
>>> regex("a[b-e]", "g")).match("abracazoo")
>>>
>>> but most regex code I've seen mentions the string first and the regex
>>> second. So I dropped that idea.
> [snip]
>>> Now, match() is likely to be called very often so I'm considering:
>>>
>>> foreach (e; "abracazoo" ~ regex("a[b-e]", "g"))
>>>    writeln(e);
>>>
>>> In general I'm weary of unwitting operator overloading, but I think this
>>> case is more justified than others. Thoughts?
>> No.  ~ means matching in Perl.  In D it means concatenation.  This
>> special case is not special enough to warrant breaking D's convention,
>> in my opinion.  It also breaks D's convention that operators have an
>> inherent meaning which shouldn't be subverted to do unrelated things.
>> What about turning it around and using 'in' though?
>>
>>    foreach(e; regex("a[b-e]", "g") in "abracazoo")
>>       writeln(e);
>>
>> The charter for "in" isn't quite as focused as that for ~, and anyway
>> you could view this as finding instances of the regular expression
>> "in" the string.
> 
> I think match is pretty short, I don't see any need for any shortcut wich
> makes the code more obscure.
> 
> BTW, in case Andrei was looking for a precedent, Python uses the syntax
> like:
> regex("a[b-e]", "g")).match("abracazoo")

Yah, but since even bearophile admitted python kinda botched regexes, I 
better not consider this argument :o). The Unix toolchain invariably 
puts the string before the regex.

Andrei



More information about the Digitalmars-d mailing list