I have the following string:

<? repeat min=1 max=2 ?>[A-Z0-9]{6},< ? endrepeat ?>100,USD,Washington

No space between < and ? in actual string

I am using the following code:

boost::regex repeatRegex("<\? repeat min=[0-9]+ max=[0-9]+ \?>(.*)<\? endrepeat \?>");
std::string theString = "<? repeat min=1 max=2 ?>[A-Z0-9]{6},<? endrepeat ?>100,USD,Washington";
boost::sregex_token_iterator itr(repeatDefinition.begin(), repeatDefinition.end(), repeatRegex);
boost::sregex_token_iterator end;

std::cout << std::endl << repeatDefinition << std::endl;
for(; itr != end; ++itr)
{
    std::cout << *itr << std::endl;
}

The code never enters the for loop. I have tested the regex with the input string in rubular regex and it apparently works. Any pointers will be greatly appreciated.

有帮助吗?

解决方案

First of all, I hope you are aware of the fact that you mixed repeatDefinition and theString as identifiers?

Secondly, you need to escape \ in c++ string literals:

boost::regex repeatRegex("<\\? repeat min=[0-9]+ max=[0-9]+ \\?>(.*)<\\? endrepeat \\?>");

See it Live On Coliru

Also may I draw your attention to this answer:

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top