錨
C++只提供 4 個錨點:
^斷言字串的開頭$斷言字串的結尾\b斷言\W字元或字串的開頭或結尾\B斷言\w字元
比方說,我們想用它的符號捕獲一個數字 :
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+)" }));
}
這裡一個重要的注意事項是錨點不會消耗任何字元。