- How does the
regex_search()
functionregex_search()
? Suppose there is a string "1 2 3 4". It is necessary to display each digit from the new line. - And yet, there is a line of code
regex rx("\\d");
How to redefinerx
to another regular expression?
|
1 answer
Like that:
one)
#include <iostream> #include <string> #include <regex> int main (){ std::string s ("1 2 3 4"); std::smatch m; std::regex e ("\\d"); while (std::regex_search (s,m,e)) { std::cout << m[0] << " "; s = m.suffix().str(); } return 0; }
2)
rx = "\\d+";
- And what does the m.suffix () call do? And how, for example, to search from a certain position in a line? - Bill
- 2The following is a reference to the target sequence. In a nutshell, it returns what comes after the match. If it is not clear in words, in my example you can add
std::cout << m.suffix().str();
and see what happens - yrHeTaTeJlb - @Bill, in order to search in a substring, you can use the
regex_search
version with iterators, it will look something likestd::regex_search (s.cbegin(), s.cend(), m, e)
. Instead ofs.cbegin()
ands.cend()
pass a couple of iterators, between which there is a substring of interest to you - yrHeTaTeJlb
|