Guides on how to use Regex in C++

I have come to the next part of my program, now i will have to be able to read info from textfiles and so on.

And i have had it pointed to me that i should use Regular Expressions for this part. I have been looking for guides on how to apply the regex to my C++ code, but havent found any, just some that only work with teaching the regex.


The things i need done is pretty simple.
Search for this, take that line, cut this off, and place the rest into this variable and so on. If anyone know any easier way to do that, please tell.

Thank you.
It can be done without regexes. Could you give a little more detail about what exactly you'll be searching for?
Ok, i gave up describing every tiny bit.


Really short version;

I want to search for 'building', then copy that line, which looks like 'building *buildingname*', cut the first part off, and just keep the namepart.
The rest is basicly the same as that with some variations.
Will probably just need one or two searches, the rest i would probably be able to get by going down a line at the time. (Which i dont know how to do either. All the guides i have read about reading from file has been extremly basic and hasnt thought me anything.)
You can use the GNU regex library.
You can use PCRE.
Both are very good and easy to use.
http://www.google.com/search?q=gnu+regex
http://www.pcre.org/

However, as helios said, you could use regular string functions to do it.
Yeah, regexes for this would definitely be overkill.

To read a line from an std::ifstream into an std::string just do std::getline(file,string);
You should be able to figure out the rest.
http://www.cplusplus.com/reference/string/string/
The highlights are find() and remove().
Ok, i have been trying to think on how i should use this.
But i simply cant think clearly at all. :(

Might be because of the lack of any good guides as always. :/


How will knowing the position of something help me in taking something out of a file, when i dont know how long that string will be.?

Wasnt it possible to read line by line, then remove parts of it, and keep the rest?
Anyone?
I need those functions translated into English so that i will be able to understand.
If you know the position, you can simply make a sub-string using substr() using the position that you found and then the end of the string (you don't need to know the size). Anyway, getline() is how you get stuff out of a file a line at a time...helios basically explained what you need to use...just experiment a bit and you should be able to figure it out.
That substr thing helped a bit of the problems, but still need a bit of variation in the functions to be able to create the final part of my program.

1. Is there a command to tell the pointer to go down one line, and to the start of that line?



2. I will read a lines that looks something like this

Alfa Potatoes requirements { Hatchet, Hammer, Sickle, Drum, Flute, }
Beta Potatoes requirements { Hatchet, Sickle, }


How it is possible for me to read what is inside the birds?
Is there a command to tell it to search for this character on this line and read the information until you come to this character?

Maybe i should just post a sample of the text i am trying to read.
1. Is there a command to tell the pointer to go down one line, and to the start of that line?
To read a line from an std::ifstream into an std::string just do std::getline(file,string);

Example:
1
2
3
4
5
6
7
8
std::ifstream file("file.txt");
std::string line;
std::getline(file,line);
/*
If the file contained "This is\ntwo lines.\n", line now contains "This is". A
second call will make line contain "two lines.". A third call will clear line
and make file.good() return false.
*/


Is there a command to tell it to search for this character on this line and read the information until you come to this character?
Unless I'm misunderstanding your question, that would be std::string::find().

Maybe i should just post a sample of the text i am trying to read.
Can't hurt.
Hmm, can i use find to search for a string, then use getline to read that whole line from there on? So the getline works from the pointer set by find?


find will get me a position, but since i dont know how many strings are within the brids, i cant use only it. Unless i take the whole line, deletes the earlier info, then delete the two birds, and use what is left. But seems like a newbie way to do it.



Ok, here is one entry of the file i want to use.

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
32
33
34
35
36
37
38
39
building mainB.building //Simple by reading line then cutting the first 9 chars off.
{
    levels mainB.levels[]  //This line contains 1-9 entries that will go into mainB.levels[]
    {
        mainB.levels[] subB.type requires factions { subB.reqFac[] }  //This line contains one of the above entries, a type (castle/city) and then a list of requirements
        {
            capability
            {
             //Here will be some tough strings to crack, but i'll probably figure that one out myself.
            }
            material subB.material //From here on it is easy
            construction  subB.construction 
            cost  subB.cost 
            settlement_min subB.settlementmin
            upgrades
            {
                subB.upgrades
            }
        }
        mainB.levels[] subB.type requires factions { subB.reqFac[] } //Starts over again with the second of the earlier strings.
        {
            capability
            {
                
            }
            material subB.material
            construction  subB.construction 
            cost  subB.cost 
            settlement_min subB.settlementmin
            upgrades
            {
                subB.upgrades
            }
        }
    }
    plugins 
    {
    }
}


I added comments.
Topic archived. No new replies allowed.