Boost logo

Boost Users :

Subject: Re: [Boost-users] regular expression to extract numbers
From: Neil Sutton (neilmsutton_at_[hidden])
Date: 2013-01-29 14:24:36


I am writing a very simple program that extracts numbers from a string. The
numbers are actually lottery numbers.
So far, my program connects to a certain url and downloads a file that
contains the latest lottery results. I have managed to reach the point
where the barest amount of relevant data is contained in a std::string.

The data is in the following format - though of course the date and numbers
vary:

26-Jan-2013,2,6,21,29,34,47,11,X,X

Note I am not interested - at this stage - in the last two numbers
represented by X,X. I am only interested in the first seven numbers
following the date.

So I figured that it should be easy to write a regular expression to match
this pattern:

boost::regex pattern("\d\d\d\d,\\>(\\d{1,2})\\<,");

I have written a function called getnumbers that is declared as void
getnumbers(std::string data); This function should eventually be passed the
string (In the format above) and it should extract the first seven numbers
after the integer year in my pattern. It is not completed yet as I am stuck
now. I have asked for help before and it was suggested I use a
sregex_token_iterator - so the code below is just an effort to experiment
with this that I found in the documentation.

--------- function definition ---------
#include <string>
#include <iostream
#include <boost/regex.hpp>

using namespace std;

void getnumbers(string data)
{
boost::regex pattern("\d\d\d\d,\\>(\\d{1,2})\\<,");
boost::sregex_token_iterator i(data.begin(), data.end(), pattern, -1); //
not sure what -1 does?
boost::sregex_token_iterator j;
unsigned count = 0;
while(i != j)
{
cout << *i++ << endl;
count++;
}
cout << "There were " << count << " tokens found." << endl;
return;
}

If passed the data string in the format above, count should be 9 -
representing 9 integers following the date.

How can I even get this to compile? I get 4 warnings about the pattern -
'unknown eascape sequence '\d'. and finally an error that says the linker
failed with exit code 1.
Also, what are the correct header guards to include in this function source
file?

Regards



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net