I was playing around with Boost.Regex to parse strings for words and numbers. This is what I have so far:
#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/regex.hpp>
#include <boost/range.hpp>
using namespace std;
using namespace boost;
int main()
{
regex re
(
"("
"([a-z]+)|"
"(-?[0-9]+(\.[0-9]+)?)"
")"
);
string s = "here is a list of Words. and some 1239.32 numbers to 3323 parse.";
sregex_iterator m1(s.begin(), s.end(), re), m2;
BOOST_FOREACH (const match_results<string::const_iterator>& what, make_iterator_range(m1, m2)) {
cout << ":" << what[1].str() << ":" << what.position(1) << ":" << what.length(1) << endl;
}
return 0;
}
Is there a way to tell regex to parse from a stream rather than a string? It seems like it should be possible to use any iterator.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…