eouti
eouti

Reputation: 5638

C++ regex, parsing

I'm pretty new with regexp and I can't get my function doing what I would like.
I have a long string, and I want to extract from it, 3 variables.

My string looks :

 Infoname/info :   
 Input_Device_Name GTape  Buffer_Size 16384 Acquisition_Event_Rate 163691.000000  
 Acquisition_Buffer_Rate 14873.333008 Acquisition_Succes_Rate 100.000000

And my goal is to store 163691.000000, 14873.333008 and 100.000000 in three differents variables.

What is the fastest and nicest way to do it please ?

Thank you,
eo

Upvotes: 0

Views: 536

Answers (2)

Component 10
Component 10

Reputation: 10467

You could use the following regex to look for it:

"Input_Device_Name\s+GTape\s+Buffer_Size\s+[0-9.]+\s+Acquisition_Event_Rate\s+([0-9.]+)\s+Acquisition_Buffer_Rate\s+([0-9.]+)\s+Acquisition_Succes_Rate\s+([0-9.]+)"

This should catch the three values assuming that your text stays the same and that your numbers always take this form (i.e. are positive and not in exponential form.) Note that only the last three numbers are captured by putting brackets round them.

If you use boost regex, you could do something like this:

#include <boost/regex.hpp>

...

boost::smatch what;
static const boost::regex pp("Input_Device_Name\\s+GTape\s+Buffer_Size\\s+[0-9.]+\\s+Acquisition_Event_Rate\\s+([0-9.]+)\\s+Acquisition_Buffer_Rate\\s+([0-9.]+)\\s+Acquisition_Succes_Rate\\s+([0-9.]+)");
if ( boost::regex_match(inputTextString, what, pp) )
{
    if ( what.size() == 4 )
    {
         double d1 = strtod(static_cast<const string&>( what[1] ).c_str(), NULL, 0);
         double d2 = strtod(static_cast<const string&>( what[2] ).c_str(), NULL, 0);
         double d3 = strtod(static_cast<const string&>( what[3] ).c_str(), NULL, 0);

         // These are your doubles, do some stuff with them.
    }
}

Where inputTextString contains the line of text you want to parse, so if this is coming from a file say, you would want to place this code in a loop. The what variable is a vector of all the matching text though what[0] contains the whole line and so can be ignored unless you need it. Last but not least, remember to double escape the 'space' character class otherwise it will already be processed (or generate an error or warning) by the compiler prior to being presented to the regex processor. Also, please note that I've not had time to compile this, though it is based on working code

Watch out for trailing, leading space on your input file and use ^ and $ to mark the beginning or end of the line respectively if it helps.

Upvotes: 1

Desmond Hume
Desmond Hume

Reputation: 8607

Just search for [0-9\.]+ as long as it returns any results. And, for example, if you would like to refuse 16384 as a variable you don't need, test every search result for having a dot in it.

Upvotes: 0

Related Questions