diff --git a/std/regex/internal/tests.d b/std/regex/internal/tests.d index edd7b391bb5..025ef7e44be 100644 --- a/std/regex/internal/tests.d +++ b/std/regex/internal/tests.d @@ -1106,3 +1106,15 @@ alias Sequence(int B, int E) = staticIota!(B, E); auto e = collectException!RegexException(regex(q"<[^]>")); assert(e.msg.canFind("no operand for '^'"), e.msg); } + +// bugzilla 17673 +@safe unittest +{ + string str = `<">`; + string[] regexps = ["abc", "\"|x"]; + auto regexp = regex(regexps); + auto c = matchFirst(str, regexp); + assert(c); + assert(c.whichPattern == 2); +} + diff --git a/std/regex/package.d b/std/regex/package.d index d61514c719f..bfc7d7ff30b 100644 --- a/std/regex/package.d +++ b/std/regex/package.d @@ -374,9 +374,15 @@ if (isSomeString!(S)) app.put("|"); app.put("(?:"); app.put(patterns[i]); + // terminator for the pattern + // to detect if the pattern unexpectedly ends app.put("\\"); - app.put(cast(dchar)(privateUseStart+i)); // special end marker + app.put(cast(dchar)(privateUseStart+i)); app.put(")"); + // another one to return correct whichPattern + // for all of potential alternatives in the patterns[i] + app.put("\\"); + app.put(cast(dchar)(privateUseStart+i)); } pat = app.data; }