Contains an example to take subject string, pattern and modifier from user input and perform regex match using JPCRE2.
#include <iostream>
void getLine(std::string& a) { std::getline(std::cin,a,'\n'); }
int main(){
std::string pat, mod, subject, ac_mod;
std::cout<<"Enter pattern: ";
getLine(pat);
while(true){
std::cout<<"Enter compile modifiers (eijmnsuxADJSU): ";
getLine(mod);
re.compile(pat, mod);
if(!re){std::cerr<<re.getErrorMessage()<<std::endl;continue;}
break;
}
std::cout<<"\nPattern compiled with modifiers: "<<re.getModifier();
size_t matched = 0;
rm.setRegexObject(&re)
.setNumberedSubstringVector(&vec_num)
.setNamedSubstringVector(&vec_nas)
.setNameToNumberMapVector(&vec_ntn)
;
for(;;) {
std::cout<<"\nEnter subject string (enter quit to quit): "<<std::endl;
getLine(subject);
if(subject == "quit") break;
std::cout<<"\nEnter action (matching) modifier (Ag): "<<std::endl;
getLine(ac_mod);
matched = rm.setSubject(subject)
.addModifier(ac_mod)
.match();
std::cout<<"\nTotal number of matches: "<<matched<<std::endl;
if(matched){
for(size_t i=0;i<vec_num.size();++i){
std::cout<< "\n################## Match no: "<<i+1<<" ####################\n";
std::cout<<"\n-------------------------------------------------------------------------";
std::cout<< "\n--- Numbered Substrings (number: substring) for match "<<i+1<<" ---\n";
for(size_t j=0;j<vec_num[i].size();++j){
std::cout<<"\n\t"<<j<<": "<<vec_num[i][j]<<"\n";
}
std::cout<<"\n-------------------------------------------------------------------------";
std::cout<< "\n--- Named Substrings (name: substring) for match "<<i+1<<" ---\n";
for(jp::MapNas::iterator ent=vec_nas[i].begin();ent!=vec_nas[i].end();++ent){
std::cout<<"\n\t"<<ent->first<<": "<<ent->second<<"\n";
}
std::cout<<"\n-------------------------------------------------------------------------";
std::cout<< "\n--- Name to number mapping (name: number/position) for match "<<i+1<<" ---\n";
for(jp::MapNtN::iterator ent=vec_ntn[i].begin();ent!=vec_ntn[i].end();++ent){
std::cout<<"\n\t"<<ent->first<<": "<<ent->second<<"\n";
}
}
}
else std::cout<<"\nNo match found\n";
}
return 0;
}