STL regex get position of match within string


I am trying to get the position of the matches of "Un" in string "Unseen University" in c++ code using STL regex. How can I do this.

i.e. I have following:


std::vector<std::cmatch*>* matches = new std::vector<std::cmatch*>*;
intMyReturnVal = regex("Unseen University", "Un", matches);

I can access a match by


and get an Iterator to the beginning of the match by


How can I get the relative position of the match within the entire target string (i.e. "Unseen University", where, in this case, the positions should be 0 and 7)??

Thanks!! :)
Last edited on
You would have to store your original string in a string variable, or at least store a pointer to it in a pointer variable, and keep it around so you can call distance() or substract the pointers later. Or you could have your regex-matching function calculate these positions and return them.

BTW, "new std::vector" is kinda crazy. Why don't you just make your function return a vector of strings (or strings, position pairs if you like?)
Topic archived. No new replies allowed.