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

Leandro Lucarella llucax at gmail.com
Thu Feb 19 05:06:14 PST 2009


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")

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------
<Palmer> recien estuvimos con el vita... se le paro yo lo vi
<Luca> ???????????????????????????????????????????????????????
<Palmer> sisi, cuando vio a josefina
<Luca> y quién es josefina?
<Palmer> Mi computadora nuevaaaaa



More information about the Digitalmars-d mailing list