Boost logo

Boost Users :

Subject: Re: [Boost-users] Regular expression problem using literals
From: Eric Niebler (eric_at_[hidden])
Date: 2012-12-11 13:17:27


On 12/11/2012 10:03 AM, Neil Sutton wrote:
> Hello, I am learning the basics of regular expression syntax and have
> been looking at the following example:
> #include <iostream>
> #include <fstream>
> #include <string>
> #include <boost/regex.hpp>
> #include <boost/xpressive/xpressive.hpp>
>
> using namespace boost::xpressive;
>
> int main()
> {
> std::ifstream input("myfile");
> if(input)
> std::cout << "Success\n";
> else
> std::cout << "fail\n";
>
> std::string line;
> std::string data;
>
> while (getline(input, line))
> {
> data += line;
> }
>
> std::string const &ref = data;
> mark_tag day(1), month(2), year(3), delim(4);
> sregex date = (month= repeat<1,2>(_d)) // find the month ...
> >> (delim= (set= '/','-')) // followed by a
> delimiter ...
> >> (day= repeat<1,2>(_d)) >> delim // and a day
> followed by the same
> >> (year= repeat<1,2>(_d >> _d)); // and the year.
> smatch what;
>
> if( regex_search(ref, what, date ) )
> {
> std::cout << what[0] << '\n'; // whole match
> std::cout << what[day] << '\n'; // the day
> std::cout << what[month] << '\n'; // the month
> std::cout << what[year] << '\n'; // the year
> std::cout << what[delim] << '\n'; // the delimiter
> }
> return 0;
> }
>
> My problem is very simple and I am sorry to be asking about it. I want
> to include some literal wording in my regular expression. For example,
> before the program outputs the date it also searches for the words
> "Football results for" - (then date is output). Please can you show me
> how to prefix this regular expression with the literal words? My
> compiler see the 'for' as a c++ keyword, without more.

How about:

sregex date = "Football results for "
>> (month= repeat<1,2>(_d))
>> (delim= (set= '/','-'))
>> (day= repeat<1,2>(_d)) >> delim
>> (year= repeat<1,2>(_d >> _d));

?

-- 
Eric Niebler
BoostPro Computing
http://www.boostpro.com

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