0

So i'm trying to make a program that searches for the number of <nodes in a file using regex_match. I've jumped through all the hoops but nothing seems to turn up a match.

string str;
int numNodes = 0;

ifstream infile("map1.osm");
if(infile.is_open())
{
    regex node("^ <node .+ .+ .+ .+ .+ .+ .+ .+ .+>$");

    while(getline(infile, str, '\n'))
    {
        cout << str << endl;
        if(regex_match(str, node))
        {
            numNodes++;
        }
    }
    cout << numNodes << endl;
}
    infile.close();

The line's format that I'm trying to match is like this

 <node id="38904495" visible="true" version="2" changeset="7460681" timestamp="2011-03-05T08:22:09Z" user="v12mike" uid="96640" lat="52.1823213" lon="-0.1499624"/>

If regex_match finds a match, it should increase numNodes by 1. At the end it always prints out 0.

  • Looks like you are trying to parse xml like `` data using regex.. bad idea: https://stackoverflow.com/a/1732454/1043152 – JATothrim Feb 17 '21 at 03:54
  • maybe dupe: https://stackoverflow.com/questions/31110474/regex-not-working-as-expected-with-c-regex-match – NathanOliver Feb 17 '21 at 04:11
  • Does this answer your question? [Regex not working as expected with C++ regex\_match](https://stackoverflow.com/questions/31110474/regex-not-working-as-expected-with-c-regex-match) – xskxzr Feb 17 '21 at 07:42
  • Work as expected [here](http://coliru.stacked-crooked.com/a/41005409105ba28b). – Jarod42 Feb 17 '21 at 09:00

0 Answers0