【发布时间】:2010-10-02 05:42:22
【问题描述】:
我正在使用 Boost.Regex 来解析字符串中的单词和数字。这是我目前所拥有的:
#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\t 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;
}
有没有办法告诉正则表达式从流而不是字符串进行解析?似乎应该可以使用任何迭代器。
【问题讨论】:
-
您可以在没有
+的情况下仅通过"a" "b"连接字符串吗?哇我从没见过……这是标准吗? -
是的,这在 C 和 C++ 中一直是标准的。您可以像这样连接字符串常量,但不能连接 C++ std::strings。