Regex to capture both digit values in string

Imagine I will be reading an XML file. Like the lines used in the example below. I need to capture the value of x and y for each line. If possible, avoiding an iterator. Can you please give me a hint on how to solve this (if possible without an iterator).



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <regex>
#include <string>
 
using namespace std;

int main()
{   
    int x;
    int y;
    
    string example1= "<Point x=\"0\" y=\"1\" />";
    string example2= "       x=\"2\" y=\"3\" />";
    string example3= "</Point>";
   
    const std::regex re("(\"([0-9]+)\")");
    
    std::smatch sm;
    if (std::regex_search(example1, sm, re))
    {
        //Match is working to detect numbers
        cout << "Match!";
        //Now I would expect to print 0, 1 for example:
        cout << sm[1];
        cout << sm[1];
       
    }
    
    
    return 0;
}
there are a lot of ways to do it avoiding iterators; you can append a '\0' and use strstr on a string variable, which is a lot like find without the iterators.
search for x=, search again for your \" pair and extract between the pair, that is your number.

its probably doable with regx too, but I do not use those enough and it takes me a bit to nail down the symbols needed to make it do what I want. you are on your own there but its certainly doable.
Last edited on
Is this for an exercise? If not, why not use an XML parser?

Andy
Topic archived. No new replies allowed.