Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to iterate all regex matches in a std::string with their starting positions in c++11 std::regex?

Tags:

c++

regex

gcc

c++11

I know two ways of getting regex matches from std::string, but I don't know how to get all matches with their respective offsets.

#include <string>
#include <iostream>
#include <regex>
int main() {
  using namespace std;
  string s = "123 apples 456 oranges 789 bananas oranges bananas";
  regex r = regex("[a-z]+");
  const sregex_token_iterator end;
  // here I know how to get all occurences
  // but don't know how to get starting offset of each one
  for (sregex_token_iterator i(s.cbegin(), s.cend(), r); i != end; ++i) {
    cout << *i << endl;
  }
  cout << "====" << endl;
  // and here I know how to get position
  // but the code is finding only first match
  smatch m;
  regex_search ( s, m, r );
  for (unsigned i=0; i< m.size(); ++i) {
    cout << m.position(i) << endl;
    cout << m[i] << endl;
  }
}
like image 948
rsk82 Avatar asked May 13 '14 00:05

rsk82


1 Answers

First of all, why token iterator? You don't have any marked subexpressions to iterate over.

Second, position() is a member function of a match, so:

#include <string>
#include <iostream>
#include <regex>
int main() {
    std::string s = "123 apples 456 oranges 789 bananas oranges bananas";
    std::regex r("[a-z]+");

    for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
                            i != std::sregex_iterator();
                            ++i )
    {
        std::smatch m = *i;
        std::cout << m.str() << " at position " << m.position() << '\n';
    }
}

live at coliru: http://coliru.stacked-crooked.com/a/492643ca2b6c5dac

like image 74
Cubbi Avatar answered Nov 16 '22 00:11

Cubbi