-2
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <regex>

using namespace std;

int main(int argc, char* argv[]) {

     string test = "<html><div><script>var link = "http://example.com/?key=dynamic_key";</script></div></html>";

     regex re("http://example.com/(*)");
     smatch match;

     if (regex_search(test, match, re)) {

     cout<<"OK"<<endl;

     }

     return 0;
}

The command for this compile.

root# g++ test.cpp -o test -std=gnu++11

This program not working. How do I get the link (use regex) from the html code? Please, help me.

Niall
  • 30,036
  • 10
  • 99
  • 142

3 Answers3

0

Your string construction is incorrect, see the " escaping:

 string test = "<html><div><script>var link = \"http://example.com/?key=dynamic_key\";</script></div></html>";

And I would use this regex:

http:\/\/example.com[^"]*

which select only this:

http://example.com/?key=dynamic_key

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
0

I see two problems with your code.

The first is you are trying to put quotes " inside quotes without escaping them.

You need to do: "escape your \"quotes\" properly" (note the \"):

Also your regex was not quite right, * needs to follow a matchable character (like [^"] meaning not a quote):

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <regex>

using namespace std;


int main(int argc, char* argv[]) {

     //string test = "<html><div><script>var link = "http://example.com/?key=dynamic_key";</script></div></html>";
     string test = "<html><div><script>var link = \"http://example.com/?key=dynamic_key\";</script></div></html>";

     //regex re("http://example.com/(*)");
     regex re("http://example.com/([^\"]*)"); // NOTE the escape \"
     smatch match;

     if (regex_search(test, match, re)) {

         cout<<"OK"<<endl;
         cout << match.str(1) << '\n'; // first capture group

     }

return 0;

}

Output:

OK
?key=dynamic_key
Galik
  • 47,303
  • 4
  • 80
  • 117
  • Then he'll match `http://example.com/?key=dynamic_key\"; – Thomas Ayoub Jun 23 '15 at 11:49
  • @Thomas The question is not about what get's matched, I was just pointing out the errors. However I have changed the example to do what I *guess* the OP is driving at. – Galik Jun 23 '15 at 11:55
0

I think there are two errors here:

  1. The test string is incorrectly delimited. Try use raw string literals.
  2. The regex isn't quite right either (I assume you want to match the full link).

Further there is one more warning, regex and html don't always work well together.

Sample code listing

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <regex>
using namespace std;
int main(int argc, char* argv[]) {
    string test = R"(<html><div><script>var link = "http://example.com/?key=dynamic_key";</script></div></html>)";

    regex re( R"(http://example\.com/[^"]*)" );
    smatch match;

    if (regex_search(test, match, re)) {
        cout << "OK" << endl;
        for (auto i : match) {
            cout << i << endl;
        }
    }
    return 0;
}

And the output here is;

OK
http://example.com/?key=dynamic_key

See here for a live sample.

Niall
  • 30,036
  • 10
  • 99
  • 142