regex - match/matchAll and bmatch - different output

Ivan Kazmenko via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Dec 30 03:06:55 PST 2015


Hi,

While solving Advent of Code problems for fun (already discussed 
in the forum: 
http://forum.dlang.org/post/cwdkmblukzptsrsrvdkr@forum.dlang.org), I ran into an issue.  I wanted to test for the pattern "two consecutive characters, arbitrary sequence, the same two consecutive characters".  Sadly, my solution using regular expressions gave a wrong result, but a hand-written one was accepted.

The problem reduced to the following:

import std.regex, std.stdio;
void main ()
{
	writeln (bmatch   ("abab",  r"(..).*\1"));  // [["abab", "ab"]]
	writeln (match    ("abab",  r"(..).*\1"));  // [["abab", "ab"]]
	writeln (matchAll ("abab",  r"(..).*\1"));  // [["abab", "ab"]]
	writeln (bmatch   ("xabab", r"(..).*\1"));  // [["abab", "ab"]]
	writeln (match    ("xabab", r"(..).*\1"));  // []
	writeln (matchAll ("xabab", r"(..).*\1"));  // []
}

As you can see, bmatch (usage discouraged in the docs) gives me 
the result I want, but match (also discouraged) and matchAll (way 
to go) don't.

Am I misusing matchAll, or is this a bug?

Ivan Kazmenko.



More information about the Digitalmars-d-learn mailing list