3

It is supposed to match "abababab" since "ab" is repeated more than two times consecutively but the code isn't printing any output. Is there some other trick in using regex in C++.

I tried with other languages and it works just fine.

#include<bits/stdc++.h>

int main(){

  std::string s ("xaxababababaxax");
  std::smatch m;
  std::regex e ("(.+)\1\1+");   

   while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}
5
  • Note that it matches xa as well.. Commented Jun 28, 2015 at 10:45
  • Isn't (.+) just greedy and matches the whole string? Therefore the \1 does not make any sense Commented Jun 28, 2015 at 10:46
  • 1
    Don't use <bits/...>, it's not portable and you have no guarantee it will work tomorrow. Also, you may need to upgrade gcc to the latest and greatest 5.x version, earlier ones don't have a working regexp implementation. Commented Jun 28, 2015 at 10:48
  • 4
    Use a raw string R"((.+)\1\1+)" or escape the backslashes "(.+)\\1\\1+" Commented Jun 28, 2015 at 10:50
  • @Ed, backtracking will make .+ match successively less until the \1 matches. It's a fairly common idiom in regex. Commented Apr 11, 2018 at 8:06

1 Answer 1

6

Your problem is your backslashes are escaping the '1''s in your string. You need to inform std::regex to treat them as '\' 's. You can do this by using a raw string R"((.+)\1\1+)", or by escaping the slashes, as shown here:

#include <regex>
#include <string>
#include <iostream>


int main(){

  std::string s ("xaxababababaxax");
  std::smatch m;
  std::regex e ("(.+)\\1\\1+");

   while (std::regex_search (s,m,e)) {
    for (auto x:m) std::cout << x << " ";
    std::cout << std::endl;
    s = m.suffix().str();
  }

  return 0;
}

Which produces the output

abababab ab 
Sign up to request clarification or add additional context in comments.

3 Comments

How do I add a flag to return the minimum string that matches for example aaaaaa should match a repeated 6 times not aa repeated three times?
Hmm. I have to admit you're doing stuff with regex's I haven't tried before. You could try using the "non-greedy-operator" stackoverflow.com/questions/11898998/regex-match-non-greedy , which is '?', and see if that gets the results you want. Let me know what you find out!
No problem. Regex's can be a pain in the butt, but they are a really worthwhile skill to develop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.