J
J
Junior0072016-11-14 17:04:36
C++ / C#
Junior007, 2016-11-14 17:04:36

How to find multiple occurrences in one string?

Suppose there is a line in which there are N links, I wrote a regular expression to search for links, it works.
Is it possible to somehow get by with one reg. expression and immediately extract all the addresses from the string?
Or is it necessary to write a loop here and until the line is over, start a new search from the previous position?

std::string str = "<html><body><a href=\"url1\">name link1</a><bla bla bla><a href=\"url2\">name link2</a></body></html>";
  std::smatch res;
  std::regex reg("(<a href=\")([\\w\\s]*)(\">)(.*)(</a>)");

  std::regex_search(str, res, reg);
  std::cout << res[2] << std::endl;
  std::regex_search(str, res, reg);
  std::cout << res[2] << std::endl;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2016-11-14
@xpert13

If you want to get some kind of array of links as a result, then only a loop.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question