Reputation: 335
I've spent most of the day learning about regular expressions in an attempt to parse configuration files made by my program. Currently, the config file vaguely resembles an INI file, but it will be expanded later. It's structured like this
> ##~SECTIONNAME~##
> #KEY#value/#
> #KEY#value/#
> #KEY#value/#
> ##~ANOTHERSECTION~##
> #KEY#value/#
> #KEY#value/#
> #KEY#value/#
What I'm trying to do is get the section names back as strings. My regular expression is #{2}~(.*)~#{2} and it's worked fine on an online perel regex tester. But when I run it through c++, I get odd results.
split_regex(sectionList,file,regex("#{2}~(.*)~#{2}"));
sectionList is a temporary data holder that will hold a list of the section names. File is a string with all that text from a loaded configuration file. What it currently does is give me a blank first index. The second index holds a string with everything below the LAST section.
My ultimate goal is to have a vector of pairs, one holding the section list's text, the other holding another vector. The second vector will hold instances of a class that will hold the key and value (or maybe just another pair).
What's a good way to go about this? I understand how to write regular expressions just fine now. But even after looking at the documentation of regular expressions in boost, I'm still not quite sure how to go about USING said regular expressions.
Thanks for reading my question. I really appreciate you taking the time to do that. Any help would be appreciated.
Upvotes: 3
Views: 288
Reputation: 4042
Have a look at the documentation for the Boost split_regex
-function. The provided regexp is used as a delimiter to split the string.
Your regexp matches the part you want, but if you really want to use the split_regex
it should match everything between your section names.
The latest version of C++ (C++ 11) provides new features concerning regular expressions. This particular function is able to determine if a string matches a given regexp and return all the matches. Have a look at the example provided on the last linked page.
Upvotes: 2