regex error
Dmitry Olshansky
dmitry.olsh at gmail.com
Mon Nov 24 07:50:25 UTC 2025
On Sunday, 23 November 2025 at 12:43:05 UTC, Jabba Laci wrote:
> I'm working on an Advent of Code problem (2015, Day 5, Part 2),
> and my code doesn't work in D. In Python I get the correct
> result.
>
> Here is a string:
>
> const s = "xdwduffwgcptfwad";
>
> The instructions says: "It contains a pair of any two letters
> that appears at least twice in the string without overlapping,
> like xyxy (xy) or aabcdefgaa (aa), but not like aaa (aa, but it
> overlaps)."
>
> Here, "fw" appears twice. My D code:
>
> auto m1 = matchFirst(s, regex(r"(..).*\1"));
>
> returns an empty m1 object. What am I doing wrong? The same
> regex works in Python.
Author of std.regex here. Indeed it’s unfortunate that the main
engine doesn’t support all cases of backreferences. It’s been a
while since I touched std, but the fix should be mostly
strightforward - use simple backtracking for this cases.
std.regex has backtracking but I think it’s also augmented with
certain tricks to avoid exponential behaviour. Those need to be
reverted, then std.regex could just select simple backtracking
for patterns that have backreferences.
—
Dmitry Olshansky
More information about the Digitalmars-d-learn
mailing list