0

I have a very simple question, but I can't figure out the answer.

I have a suffix string and I want to extract the prefix of the substring if it ends with the suffix using c++ regular expressions and submatch.

For e.g.; if I have a string "abc def" and my regex is "(.+) def"; I want to return "abc":-

However, when I try to compute it using sub_match function, I get the entire string "abc def". Here's my code :-

int main() {
  regex pattern("(.+) def");
  smatch a;
  string s = "pqr def";
  bool b = regex_match(s, a, pattern);
  cout << a.size() << a[0] << ";" << a[1] << ";";
}

Output :-
2pqr def;pqr def;

thanks, Amit

Amit Gupta
  • 451
  • 5
  • 18

0 Answers0