1

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.

sehe
  • 374,641
  • 47
  • 450
  • 633
Abhijit
  • 13
  • 2

1 Answers1

0

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:

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633
  • Ofcourse! I am planning to use regex to compare two xml files (one file containing appropriate regex tags). The idea is really to treat the XML as just another text file. Do you think it will work? – Abhijit Mar 05 '14 at 14:40
  • Soooo... lemme get this straigth. You're using regexen to match contents of two xml files containing... regex tags? – sehe Mar 05 '14 at 14:41
  • One file will contain the regex while the other will have the actual contents. The first file is a template file that would be used to check the validity of the contents of the actual xml file. – Abhijit Mar 05 '14 at 16:33
  • You are aware of tools like XSD validation and e.g. xmllint right? Why invent the wheel, badly? – sehe Mar 05 '14 at 16:34
  • It is going to be used for a testing tool which should be able to accept any text file to validate (not limited to xml only) and will have a corresponding template file (having appropriate regexes). So the tool should not even care about what file it needs to validate as long as it gets a proper baseline template file having all the correct regexes. – Abhijit Mar 05 '14 at 16:41