Program to find all match of a regex in a string

04 Sep, 2018
Prerequisite: smatch | Regex (Regular Expressions) in C++
Given a regex, the task is to find all regex matches in a string.
Without using iterator:// C++ program to find all the matches#include <bits/stdc++.h>using namespace std;int main(){    string subject("My GeeksforGeeks is my "                     "GeeksforGeeks none of your GeeksforGeeks");      // Template instantiations for    // extracting the matching pattern.    smatch match;    regex r("GeeksforGeeks");    int i = 1;    while (regex_search(subject, match, r)) {        cout << "\nMatched string is " << match.str(0) << endl             << "and it is found at position "              << match.position(0)<<endl;         i++;          // suffix to find the rest of the string.        subject = match.suffix().str();    }    return 0;}Output:Matched string is GeeksforGeeks
and it is found at position 3

Matched string is GeeksforGeeks
and it is found at position 7

Matched string is GeeksforGeeks
and it is found at position 14
Note: Above code is running perfectly fine but the problem is input string will be lost.
// C++ program to find all the matches#include <bits/stdc++.h>using namespace std;int main(){    string subject("My GeeksforGeeks is my "                     "GeeksforGeeks none of your GeeksforGeeks");      // Template instantiations for    // extracting the matching pattern.    smatch match;    regex r("GeeksforGeeks");    int i = 1;    while (regex_search(subject, match, r)) {        cout << "\nMatched string is " << match.str(0) << endl             << "and it is found at position "              << match.position(0)<<endl;         i++;          // suffix to find the rest of the string.        subject = match.suffix().str();    }    return 0;}
Matched string is GeeksforGeeks
and it is found at position 3

Matched string is GeeksforGeeks
and it is found at position 7

Matched string is GeeksforGeeks
and it is found at position 14

Note: Above code is running perfectly fine but the problem is input string will be lost.
Using iterator:Object can be constructed by calling the constructor with three parameters: a string iterator indicating the starting position of the search, a string iterator indicating the ending position of the search, and the regex object. Construct another iterator object using the default constructor to get an end-of-sequence iterator.#include <bits/stdc++.h>using namespace std;int main(){    string subject("geeksforgeeksabcdefghg"                   "eeksforgeeksabcdgeeksforgeeks");      // regex object.    regex re("geeks(for)geeks");      // finding all the match.    for (sregex_iterator it = sregex_iterator(subject.begin(), subject.end(), re);         it != sregex_iterator(); it++) {        smatch match;        match = *it;        cout << "\nMatched  string is = " << match.str(0)             << "\nand it is found at position "             << match.position(0) << endl;        cout << "Capture " << match.str(1)             << " at position " << match.position(1) << endl;    }    return 0;}Output:Matched  string is = geeksforgeeks
and it is found at position 0
Capture for at position 5

Matched  string is = geeksforgeeks
and it is found at position 21
Capture for at position 26

Matched  string is = geeksforgeeks
and it is found at position 38
Capture for at position 43

#include <bits/stdc++.h>using namespace std;int main(){    string subject("geeksforgeeksabcdefghg"                   "eeksforgeeksabcdgeeksforgeeks");      // regex object.    regex re("geeks(for)geeks");      // finding all the match.    for (sregex_iterator it = sregex_iterator(subject.begin(), subject.end(), re);         it != sregex_iterator(); it++) {        smatch match;        match = *it;        cout << "\nMatched  string is = " << match.str(0)             << "\nand it is found at position "             << match.position(0) << endl;        cout << "Capture " << match.str(1)             << " at position " << match.position(1) << endl;    }    return 0;}
Matched  string is = geeksforgeeks
and it is found at position 0
Capture for at position 5

Matched  string is = geeksforgeeks
and it is found at position 21
Capture for at position 26

Matched  string is = geeksforgeeks
and it is found at position 38
Capture for at position 43

CPP-Library
Pattern Searching
Strings
Strings
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Reverse the substrings of the given String according to the given Array of indices
Check if the given string is shuffled substring of another string
How to validate GUID (Globally Unique Identifier) using Regular Expression
How to check Aadhaar number is valid or not using Regular Expression
How to validate pin code of India using Regular Expression
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Check for Balanced Brackets in an expression (well-formedness) using Stack