On 14.03.2012 0:05, bearophile wrote:
Dmitry Olshansky:
It's about time to break this gloomy aura, and show that std.regex is
actually easy to use, that it does the thing and has some nice extras.
This seems a good moment to ask people regarding this small problem, that we
have already discussed a little in Bugizilla (there is a significant need to
show here some Bugzilla discussions):
http://d.puremagic.com/issues/show_bug.cgi?id=7260
Yeah, it's prime thing that I regret when thinking of current API.
The problem is easy to show:
import std.stdio: write, writeln;
import std.regex: regex, match;
void main() {
string text = "abc312de";
foreach (c; text.match("1|2|3|4"))
write(c, " ");
writeln();
foreach (c; text.match(regex("1|2|3|4", "g")))
write(c, " ");
writeln();
}
It outputs:
["3"]
["3"] ["1"] ["2"]
In my code I have seen that usually the "g" option (that means "repeat over the
whole input") is what I want. So what do you think about making "g" the default?
I like the general idea of foreach on match to work intuitively.
Yet I'm not convinced to use extra flag as "non-global".
I'd propose to yank "g" flag entirely assuming all regex are global, but
that breaks code in a lot of subtle ways. Problems of using global flag
by default:
1. Generic stuff:
assert(equal(match(...), someOtherRange)); //normal regex silently
becomes global, quite unexpectedly
2. replace that then have to be 2 funcs - replaceFirst, replaceAll or we
are back to the problem of extra flag.
I'm thinking there is a path through opApply to allow foreach iteration
of non-global regex as if it had global flag, yet not getting full range
interface. It's hackish but so far it's as best as it gets.
This request is not as arbitrary as it looks, if you compare to the older API.
See Bug 7260 for more info.
--
Dmitry Olshansky