JPCRE2  10.32.01
C++ wrapper for PCRE2 library
test_match2.cpp File Reference
#include <iostream>
#include "jpcre2.hpp"
+ Include dependency graph for test_match2.cpp:

Detailed Description

Contains an example to take subject string, pattern and modifier from user input and perform regex match using JPCRE2.

#include <iostream>
#include "jpcre2.hpp"
void getLine(std::string& a) { std::getline(std::cin,a,'\n'); }
int main(){
jp::VecNum vec_num; //Vector to store numbered substring vectors.
jp::VecNas vec_nas; //Vector to store named substring Map.
jp::VecNtN vec_ntn; //Vector to store Named substring to Number Map.
std::string pat, mod, subject, ac_mod;
//create an object
std::cout<<"Enter pattern: ";
std::cout<<"Enter compile modifiers (eijmnsuxADJSU): ";
//Compile pattern
re.compile(pat, mod);
//Check if the pattern was compiled successfully, continue the loop otherwise
std::cout<<"\nPattern compiled with modifiers: "<<re.getModifier();
size_t matched = 0;
rm.setRegexObject(&re) //set associated Regex object
.setNumberedSubstringVector(&vec_num) //pointer to numbered substring vector
.setNamedSubstringVector(&vec_nas) //pointer to named substring vector
.setNameToNumberMapVector(&vec_ntn) //pointer to name-to-number map vector
//.match() //Let's do the match later
for(;;) { //forever loop
std::cout<<"\nEnter subject string (enter quit to quit): "<<std::endl;
if(subject == "quit") break;
std::cout<<"\nEnter action (matching) modifier (Ag): "<<std::endl;
//Now let's do the match
matched = rm.setSubject(subject) //subject
.addModifier(ac_mod) //add modifier
.match(); //Now perform the match
//Now let's access the matched data
//Each of these vectors contains maps, except the VecNum which contains vectors.
//Each element in the vector specifies a particular match
//First match is the vector element 0, second is at index 1 and so forth
//A map or sub vector for a vector element, i.e for a match, contains all of its substrings/captured groups
//The first element of the map or sub vector is capture group 0 i.e total match
std::cout<<"\nTotal number of matches: "<<matched<<std::endl;
for(size_t i=0;i<vec_num.size();++i){
std::cout<< "\n################## Match no: "<<i+1<<" ####################\n";
//This vector contains vectors of substrings or captured group index by index.
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";
//This vector contains maps with name as the key and the corresponding substring as the value
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";
//This vector contains maps with name as the key and number as the value
//i.e the number (of substring) can be accessed with the name for named substring.
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;
Md Jahidul Hamid
std::vector< NumSub > VecNum
Vector of matches with numbered substrings.
Definition: jpcre2.hpp:1279
Main header file for JPCRE2 library to be included by programs that uses its functionalities....
Provides public constructors to create Regex object.
Definition: jpcre2.hpp:3550
struct to select the types.
Definition: jpcre2.hpp:1238
std::vector< MapNtN > VecNtN
Vector of substring name to substring number map.
Definition: jpcre2.hpp:1275
Provides public constructors to create RegexMatch objects.
Definition: jpcre2.hpp:1375
std::vector< MapNas > VecNas
Vector of matches with named substrings.
Definition: jpcre2.hpp:1273