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

Michel Fortin michel.fortin at michelf.com
Thu Feb 19 05:27:54 PST 2009


On 2009-02-19 00:35:20 -0500, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> auto s = sub("abracazoo", regex("a([b-e])", "g"), "A$1");

I don't like `sub`, I mean the name. Makes me think of substring more 
than substitute. My choice would be to reuse what we have in std.string 
and augment it to work with regular expressions:

	auto s = replace("abracazoo", regex("a([b-e])", "g"), subex("A$1"));

This way it works consistently whether you're using a string or a 
regular expression: just replace any pattern string with regex(...) and 
any replacement string with subex(...) -- "substition-expression" -- 
when you want them to be parsed as such. Omitting subex in the above 
would make it a plain string replacement for instance (this way it's 
easy to place use a variable there).

These functions should allow easy substitution of any string or regex 
pattern with another algorithm for matching the pattern.

And there's not way to get a range of matches using std.string, but 
there should be, and it should follow the same rule as above: 
supporting strings and regex consistently. (Using the `in` operator as 
suggested by Bill Baxter seems a good fit for this function.)

And if any of you complains about the extra verbosity, here's what I suggest:

	auto s = replace("abracazoo", re"a([b-e])"g, se"A$1");

Yes, syntaxic sugar for declaring regular expressions.


> Two other syntactic options are available:
> 
> "abracazoo".match(regex("a[b-e]", "g")))
> "abracazoo".match("a[b-e]", "g")

I despise the second one, because if you omit regex(...) it makes me 
think you're checking for string matches, not expression matches. 
There's nothing in the name of the funciton telling you you're dealing 
with a regular expression, so it could easily get confusing.


-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list