Syed Babu wrote:
regex r1("<.*>.*</\\1>"); regex r2("<(.*)>(.*)</(\\1)>"); smatch s1,s2; string str = "Hello,world<book>C++</book>"; regex_search( str,s1,r1); regex_search( str,s2,r2);Both the seraches return the result as true but s1.size is not equal to s2.size. What does this mean?
Parentheses introduce capture groups. match_results object represents an array of submatches - one (at index zero) for the regular expression as a whole, and one for each capture group.
In your example, s1.size() should be 1, with s1[0] == "<book>C++</book>". s2.size() should be 4: s2[0] == "<book>C++</book>", s2[1] == "book", s2[2] == "C++", s2[3] == "book".
What significance the paranthesis apart from "capture group"?
None. Their significance is precisely in indicating a capture group.
string data = "<Person>\n""<First>Nico</First>\n""<Last>Jos</Last>\n""</Person>"; regex r("<(.*)>(.*)</(\\1)>"); sregex_iterator pos( data.cbegin( ), data.cend( ),r ) sregex_iterator end; for( ; pos != end ; ++pos ) { }The above code works fine. But passing the regex as temporary object gives assert failure while iterating the iterator. What is
the reason for the assert?
The documentation for regex_iterator constructor states: "The second constructor initializes... the stored value pregex with &re". So it stores a pointer to regex object. Naturally, said regex object must outlive regex_iterator, for the pointer to remain valid.
Igor Tandetnik