I'm writing a function which is designed to take a text like this:
%%HEADER
foo bar baz
%%BODY
foo baz baz
And return an array like this:
{"foo bar baz", "foo baz baz"}
With that in mind, I wrote the following:
string[2] separate_header_body(string input) {
string[2] separated;
auto header = matchFirst(input, regex(r"%%HEADER\n((.|\n)*)\n%%BODY", "g"));
if (header.empty()) {
throw new ParserException("No %%HEADER found.");
} else {
separated[0] = header.front();
auto bdy = matchFirst(input, regex(r"%%BODY\n((.|\n)*)", "g"));
if (bdy.empty()) {
throw new ParserException("No %%BODY found.");
} else {
separated[1] = bdy.front();
}
}
return separated;
}
However, when I try to test it with the following input:
"%%HEADER\nfoo bar baz\n%%BODY\nfoo baz baz"
The first capture is "%%HEADER\nfoo bar baz\n%%BODY
, which is clearly too much. Am I using std.regex
incorrectly for what I want?