Program to find all match of a regex in a string
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>usingnamespacestd;intmain(){string subject("My GeeksforGeeks is my ""GeeksforGeeks none of your GeeksforGeeks");// Template instantiations for// extracting the matching pattern.smatch match;regex r("GeeksforGeeks");inti = 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();}return0;}chevron_rightfilter_noneOutput: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>usingnamespacestd;intmain(){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;}return0;}chevron_rightfilter_noneOutput: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
Recommended Posts:
- Match Expression where a single special character in pattern can match one or more characters
- Get the first letter of each word in a string using regex in Java
- Extract maximum numeric value from a given string | Set 2 (Regex approach)
- Python Regex to extract maximum numeric value from a string
- Find all strings that match specific pattern in a dictionary
- regex_replace in C++ | Replace the match of a string using regex_replace
- Program to find the kth character after decrypting a string
- Program to find Smallest and Largest Word in a String
- Program to find the product of ASCII values of characters in a string
- Program to find the largest and smallest ASCII valued characters in a string
- String Range Queries to find the number of subsets equal to a given String
- Find the longest sub-string which is prefix, suffix and also present inside the string
- Find the character in first string that is present at minimum index in second string
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Find the count of palindromic sub-string of a string in its sorted form
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



