mardi 22 décembre 2015

How should you use C++11 Regex to create variables from a table stored in a text file?

This may be a fairly trivial question but I am confused about how Regex works in C++. I have a text file that looks like the following:

Weapons:    Cost  Damage  Armor
Dagger        8     4       0
Shortsword   10     5       0
Warhammer    25     6       0
Longsword    40     7       0
Greataxe     74     8       0

I want to store each weapons Name, Cost, Damage and Armor in a Struct. I tried to do that with the following code:

regex rule("(\\w+)\s(\\w+)\s(\\w+)\s(\\w+)");
smatch match;

for (int i = 0; i < 6; i++) {
    if (regex_match(lines[i], match, rule)) {
        struct Weapon Weapon;
        Weapon.Name = match[1];
        Weapon.Cost = stoi(match[2], NULL, 10);
        Weapon.Damage = stoi(match[3], NULL, 10);
        Weapon.ArmorValue = stoi(match[4], NULL, 10);
        Weapons.push_back(Weapon);
    }
}

However the if statement is always returning false.

Am I using the Regex Rule wrongly?

How should I be using Regex to store the data from that table in a struct?

Aucun commentaire:

Enregistrer un commentaire