VOOZH about

URL: https://www.geeksforgeeks.org/cpp/program-to-find-all-match-of-a-regex-in-a-string/

⇱ Program to find all match of a regex in a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to find all match of a regex in a string

Last Updated : 11 Jul, 2025

Prerequisite: smatch | Regex (Regular Expressions) in C++ Given a regex, the task is to find all regex matches in a string.

Without using iterator: 

Implementation:


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.
  • 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. 

Implementation:


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
Comment
Article Tags:
Article Tags: