|
Boost : |
From: David Abrahams (dave_at_[hidden])
Date: 2004-04-05 14:22:10
I was just writing up a simple tutorial example; finding the subject
in a set of email headers. Here's what I got:
std::string line;
boost::regex pat("^Subject: (Re: )?(.*)");
boost::smatch matches;
while (std::cin)
{
std::getline(std::cin, line);
if (boost::regex_match(line,matches, pat))
std::cout << matches[2];
}
1. There's no way to search a stream for a match because a regex
requires bidirectional iterators, so I have to do this totally
frustrating line-by-line search. I think Spirit has some kind of
iterator that turns an input iterator into something forward by
holding a cache of the data starting with the earliest copy of the
original iterator. Could something like that be added?
2. Seems to me that if match objects could be converted to bool, we
might be able to:
std::string line;
boost::regex pat("^Subject: (Re: )?(.*)");
while (std::cin)
{
std::getline(std::cin, line);
if (boost::smatch m = boost::regex_match(line, pat))
std::cout << m[2];
}
which would be much smoother to the touch. Are match objects
expensive to construct?
-- Dave Abrahams Boost Consulting www.boost-consulting.com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk