Is it possible to search for certain patterns inside std::string in C++?
I am aware of find()
, find_first_of()
and so on, but I am looking for something more advanced.
For example, take this XML line: <book category="fantasy">Hitchhiker's guide to the galaxy </book>
If I want to parse it using find()
I could do it as follows:
string line = "<book category=\"fantasy\">Hitchhiker's guide to the galaxy </book>"
size_t position = line.find('>');
std::string tmp = line.substr(0,position);
position = tmp.find(' ');
std::string node_name = tmp.substr(0, position);
std::string parameters = tmp.substr(position+1);
...
This feels very "grindy" and inefficient. It would also fail, if I had multiple tags nested inside each other, like this:
<bookstore><book category=\"fantasy\">Hitchhiker's guide to the galaxy </book></bookstore>
Is there a way to search for pattern, as in search for <*>
where *
represents any amount of characters of any value, getting the value of *
, splitting it to parameters and node name, then searching for </nodename>
and getting the value inbetween <nodename>
and </nodename>
?