64,648
社区成员
发帖
与我相关
我的任务
分享
a b c
d e f
^\w\s+
#include <string>
using namespace std;
#include <boost/regex.hpp>
int main()
{
int flag = 0;
flag |= boost::regex_constants::perl;
flag |= boost::regex_constants::no_empty_expressions;
boost::regex_constants::match_flag_type mode = boost::regex_constants::match_default;
mode |= boost::regex_constants::match_perl;
if( 1 )
mode |= boost::regex_constants::match_single_line;
else
mode &= ~boost::regex_constants::match_single_line;
// 正则表达式为^\w\s+
wstring strRegex( L"^\\w\\s+" );
boost::wregex reg( strRegex, (boost::regex_constants::flag_type_)flag );
// 源字符串为a b c 回车d e f 结束
wstring strSur = L"a b c \nd e f ";
boost::wsmatch m;
wstring::const_iterator iter = strSur.begin();
wstring::const_iterator end = strSur.end();
while( boost::regex_search( iter, end, m, reg, mode ) )
{
wstring strFound = m[0].str();
wcout << "len: " << strFound.size() << " content: " << strFound << endl;
iter = m[0].second;
}
return 0;
}
len: 3 content: a
len: 3 content: b
len: 4 content: c
len: 3 content: d
len: 3 content: e
len: 3 content: f
请按任意键继续. . .
^ab+
匹配abbabbabb
abbabbabb
也有问题。这下跟换行没关系了吧?