Regular expression woes

just jeff jeffrparsons at optusnet.com.au
Wed Jan 17 03:34:09 PST 2007


Is this a bug, or am I misunderstanding something? The code...

# import std.stdio;
# import std.regexp;
#
# int main(char[][] args) {
#     char[] string = "xfooxxxxxfoox";
#     writefln("Greedy matching:");
#     foreach (RegExp match; RegExp("x.*x").search(string))
#         writefln("%s[%s]%s", match.pre, match.match(0), match.post);
#     writefln("Conservative matching:");
#     foreach (RegExp match; RegExp("x.*?x").search(string))
#         writefln("%s[%s]%s", match.pre, match.match(0), match.post);
#     return 0;
# }

...compiled under GDC 0.21 (using the Phobos version that ships 
therewith) yields:

Greedy matching:
[xfooxxxxx]foox
Conservative matching:
[xfoox]xxxxfoox
xfoox[xx]xxfoox
xfooxxx[xx]foox

The latter part (conservative matching) makes plenty of sense to me, but 
I thought the former should have matched the whole string (i.e. read 
"[xfooxxxxxfoox]".

Is this behaviour intended?

Thanks. :)



More information about the Digitalmars-d mailing list