0

The regex_search here selects only the longest substring in this program as the output is the whole string data. Is that the default behaviour?

Additionally if i pass the string without declaring it as a string first like this

regex_search("<html><body>some data</body></html",m,samepattern)

It throws an error of type mismatch .

Additionally if i only use without the additional 2nd parameter

regex_search("some string",pattern);

it works.

The whole code is shown below

#include<string>
#include<regex>
#include<iostream>
#include<iomanip>
using namespace std;

int main()
{
   smatch m;
   string data{"<html><body>some data</body></html"};
   bool found = regex_search(data,m,regex("<.*>.*</.*>"));
   //If regex_seacrh("<html><body>some data</body></html",same as above)
   //it throws type mismatch error for this string in regex header
   cout<<(found?m.str():"not found");
   return 0;
}
Anton Savin
  • 40,838
  • 8
  • 54
  • 90
rahul tyagi
  • 643
  • 1
  • 7
  • 20

1 Answers1

2

First of all, you don't parse HTML with regex.

But if you do, you indeed have parameter mismatch in the call

smatch m;
bool found = regex_search("some data", m, regex("some regex"));

Corresponding regex_search() overload has to be:

template <class charT, class Allocator, class traits>
bool regex_search(const charT* str,
    match_results<const charT*, Allocator>& m,
    const basic_regex<charT, traits>& e,
    regex_constants::match_flag_type flags =
    regex_constants::match_default);

but m is of type smatch which is match_results<std::string::const_iterator>.

What you should do is to use cmatch instead of smatch:

cmatch m;
bool found = regex_search("some data", m, regex("some regex"));

Update as for your question about the longest match - you have to use non-greedy match qualifiers, that is e.g .*?

Community
  • 1
  • 1
Anton Savin
  • 40,838
  • 8
  • 54
  • 90