Bug 14615 – std.regex.replaceFirstInto throws exception when no match is found

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
x86_64
OS
Linux
Creation time
2015-05-22T13:18:00Z
Last change time
2016-04-07T17:02:02Z
Keywords
pull
Assigned to
nobody
Creator
wendlec

Comments

Comment #0 by wendlec — 2015-05-22T13:18:59Z
std.regex.replaceFirst returns the original input, if no match has been found. However, replaceFirstInto throws an exception, which means you have to know beforehand whether or not a replacement is possible (example code below). The same might be true of replaceAllInto, although I haven't tested it. See also this post http://forum.dlang.org/thread/[email protected] void main() { import std.stdio : writeln; import std.regex : replaceFirst, replaceFirstInto, regex; import std.array : appender; auto example = "Hello, world!"; auto pattern = regex("^Hello, (bug)"); // won't find this one auto result = replaceFirst(example, pattern, "$1 Sponge Bob"); assert(result == "Hello, world!"); // Ok. auto sink = appender!string; replaceFirstInto(sink, example, pattern, "$1 Sponge Bob"); /++ Throws: phobos/std/regex/package.d(993): invalid submatch number 1 ---------------- ./replaceFirstInto(pure @safe bool std.exception.enforce!(Exception, bool).enforce(bool, lazy const(char)[], immutable(char)[], ulong)+0x65) [0x495dc5] ./replaceFirstInto(pure @safe void std.regex.replaceFmt!(const(char)[], std.regex.Captures!(immutable(char)[], ulong).Captures, std.array.Appender!(immutable(char)[]).Appender).replaceFmt(const(char)[], std.regex.Captures!(immutable(char)[], ulong).Captures, std.array.Appender!(immutable(char)[]).Appender, bool)+0x204) [0x4b074c] ./replaceFirstInto(pure @safe void std.regex.replaceFirstInto!(std.array.Appender!(immutable(char)[]).Appender, immutable(char)[], char, std.regex.internal.ir.Regex!(char).Regex).replaceFirstInto(ref std.array.Appender!(immutable(char)[]).Appender, immutable(char)[], std.regex.internal.ir.Regex!(char).Regex, const(char)[]).__lambda5!(std.regex.Captures!(immutable(char)[], ulong).Captures, std.array.Appender!(immutable(char)[]).Appender).__lambda5(std.regex.Captures!(immutable(char)[], ulong).Captures, std.array.Appender!(immutable(char)[]).Appender)+0x39) [0x4b2051] ./replaceFirstInto(pure @trusted void std.regex.__T19replaceCapturesIntoS2463std5regex112__T16replaceFirstIntoTS3std5array17__T8AppenderTAyaZ8AppenderTAyaTaTS3std5regex8internal2ir12__T5RegexTaZ5RegexZ16replaceFirstIntoFNeKS3std5array17__T8AppenderTAyaZ8AppenderAyaS3std5regex8internal2ir12__T5RegexTaZ5RegexAxaZ9__lambda5TS3std5array17__T8AppenderTAyaZ8AppenderTAyaTS3std5regex19__T8CapturesTAyaTmZ8CapturesZ.replaceCapturesInto(ref std.array.Appender!(immutable(char)[]).Appender, immutable(char)[], std.regex.Captures!(immutable(char)[], ulong).Captures)+0x4f) [0x4b1fef] ./replaceFirstInto(@trusted void std.regex.replaceFirstInto!(std.array.Appender!(immutable(char)[]).Appender, immutable(char)[], char, std.regex.internal.ir.Regex!(char).Regex).replaceFirstInto(ref std.array.Appender!(immutable(char)[]).Appender, immutable(char)[], std.regex.internal.ir.Regex!(char).Regex, const(char)[])+0x9c) [0x4b1f94] ./replaceFirstInto(_Dmain+0x14f) [0x48ad17] ./replaceFirstInto(_D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv+0x1f) [0x4b8357] ./replaceFirstInto(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x4b82aa] ./replaceFirstInto(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).runAll()+0x30) [0x4b8310] ./replaceFirstInto(void rt.dmain2._d_run_main(int, char**, extern (C) int function(char[][])*).tryExec(scope void delegate())+0x2a) [0x4b82aa] ./replaceFirstInto(_d_run_main+0x1dc) [0x4b8224] ./replaceFirstInto(main+0x17) [0x4b2fa7] /lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xf5) [0x7f3acab8eec5] +/ }
Comment #1 by wendlec — 2015-05-22T13:23:07Z
dmd 2.067.1
Comment #2 by dmitry.olsh — 2016-04-06T13:34:33Z
Comment #3 by github-bugzilla — 2016-04-07T15:40:54Z
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/add07e7ea6815bdfa6f89b12e78f76d0ecaff6fd Fix issue 14615 - std.regex.replaceFirstInto throws exception when no match is found https://github.com/D-Programming-Language/phobos/commit/efd74a5fea9dd7c8c14bf53d192ca2236f9013ef Merge pull request #4160 from DmitryOlshansky/issue-14615 Fix issue 14615 - std.regex.replaceFirstInto throws exception when no match is found