std::vector<std::cmatch> destruction??

Hi, I have some code that inserts data into a std::vector<std::cmatch*>, as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
extern "C" {

		__declspec(dllexport) int __cdecl regex(std::string target,std::string rgx, std::vector<std::cmatch*>* matches, std::vector<int*> *ints)
{
		// this can be done using raw string literals:
// const char *reg_esp = R"([ ,.\t\n;:])";
	 std::regex rgxa = std::regex(rgx);
	 const char *targetA = target.c_str();
	 
	 

	 std::regex_iterator<const char*> it;

	 std::cmatch *match = NULL;
	 for(it = std::cregex_iterator(targetA, targetA + std::strlen(targetA), rgxa);
             it != std::cregex_iterator();
           ++it)
    {
        match = new std::cmatch(*it);
		matches->push_back(match); //it //data inserted via push_back(*match)
		std::cout << match->str() << '\n';  //this call works, displays useful information (e.g. "Un" for ("Unseen University", "Un) example, as below)

    }

	return EXIT_SUCCESS;

}
}


Output:

1
2
3
4
5

Un
Un                       





However, using a call to regex function, and THEN (from OUTSIDE the regex func) trying to display the data in the std::vector<std::cmatch*> produces output:

1
2
3
4
5
6

▌▌
▌▌
▌▌
▌▌                         



The call to regex is as:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

	intMyReturnVal = regex("Unseen University", "Un", matches, ints);



	for(int i = 0; i < matches->size(); i++)
	{
		std::cmatch match = matches->at(i);

		std::cout << match->str() << endl; //doesn't work
		
		std::cout << matches->at(i)->str() << std::endl; //doesn't work

	}

Last edited on
I believe cmatch holds pointers to a source string which is destroyed after function call
Edit: some info
http://stackoverflow.com/questions/10552884/why-do-a-c-regular-expression-code-that-works-with-cmatch-raises-an-exceptio
Last edited on
yep, match_results is a (typically), vector of sub_matches, and a sub_match is a pair of iterators into the original string (and in case of a character array, as here, they are pointers).
Last edited on
Topic archived. No new replies allowed.