// NEW CODE #include "jpcre2.hpp" #include typedef jpcre2::select jp; int main() { // SAMPLE CODE //Check if string matches the pattern /* * The following uses a temporary Regex object. */ std::cout << "\n\n" << "[[[ DEBUG ]]] BEFORE MATCH 1" << "\n\n"; if(jp::Regex("(\\d)|(\\w)").match("I am the subject")) { std::cout << "\nmatched"; } std::cout << "\n\n" << "[[[ DEBUG ]]] AFTER MATCH 1" << "\n\n"; /* * Using the modifier S (i.e jpcre2::JIT_COMPILE) with temporary object may or may not give you * any performance boost (depends on the complexity of the pattern). The more complex * the pattern gets, the more sense the S modifier makes. */ //If you want to match all and get the match count, use the action modifier 'g': std::cout << "\n\n" << "[[[ DEBUG ]]] BEFORE MATCH 2" << "\n\n"; std::cout << "\n" << jp::Regex("(\\d)|(\\w)","m").match("I am the subject","g"); std::cout << "\n\n" << "[[[ DEBUG ]]] AFTER MATCH 2" << "\n\n"; size_t count; /* * Modifiers passed to the Regex constructor or with compile() function are compile modifiers * Modifiers passed with the match() or replace() functions are action modifiers */ // Substrings/Captured groups: /* * *** Getting captured groups/substring *** * * captured groups or substrings are stored in maps/vectors for each match, * and each match is stored in a vector. * Thus captured groups are in a vector of maps/vectors. * * PCRE2 provides two types of substrings: * 1. numbered (indexed) substring * 2. named substring * * For the above two, we have two vectors respectively: * 1. jp::VecNum (Corresponding vector: jp::NumSub) * 2. jp::VecNas (Corresponding map: jp::MapNas) * * Another additional vector is available to get the substring position/number * for a particular captured group by name. It's a vector of name to number maps * * jp::VecNtN (Corresponding map: jp:MapNtN) */ // ***** Get numbered substring ***** /// jp::VecNum vec_num; jp::RegexMatch rm; jp::Regex re("(\\w+)\\s*(\\d+)","m"); count = jp::RegexMatch(&re).setSubject("I am 23, I am digits 10") .setModifier("g") .setNumberedSubstringVector(&vec_num) .match(); /* * count (the return value) is guaranteed to give you the correct number of matches, * while vec_num.size() may give you wrong result if any match result * was failed to be inserted in the vector. This should not happen * i.e count and vec_num.size() should always be equal. */ std::cout<<"\nNumber of matches: "<\\w+)\\s*(?\\d+)","m"); count = jp::RegexMatch(&re).setSubject("I am 23, I am digits 10") .setModifier("g") //.setNumberedSubstringVector(vec_num) // We don't need it in this example .setNamedSubstringVector(&vec_nas) .setNameToNumberMapVector(&vec_ntn) // Additional (name to number maps) .match(); std::cout<<"\nNumber of matches: "<=C++11) function to access map elements. /* //>=C++11 try{ ///This will throw exception because the substring name 'name' doesn't exist std::cout<<"\nCaptured group (name) of first match: "<