Anchors
suggest changeIn the default ECMAScript grammar, std::regex provides four anchors. Each is zero-width: it asserts a position and consumes no characters. With std::regex_constants::multiline, ^ and $ also match around newlines.
^asserts the start of the string$asserts the end of the string\basserts a word boundary: a position between a\wcharacter and a\Wcharacter, or between a\wcharacter and the start or end of the string\Basserts a position that is not a word boundary
Let’s say for example we want to capture a number with its sign:
auto input = "+1--12*123/+1234"s;
smatch sm;
if(regex_search(input, sm, regex{ "(?:^|\\b\\W)([+-]?\\d+)" })) {
do {
cout << sm[1] << endl;
input = sm.suffix().str();
} while(regex_search(input, sm, regex{ "(?:^\\W|\\b\\W)([+-]?\\d+)" }));
}
An important note here is that the anchor does not consume any characters.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents