Boost logo

Boost Users :

Subject: [Boost-users] Regular expression problem using literals
From: Neil Sutton (neilmsutton_at_[hidden])
Date: 2012-12-11 13:03:39


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.
Kind 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