我正在使用boost regex_match,并且在匹配没有制表符时出现问题。 我的测试应用程序如下所示:Boost正则表达式与标签不匹配
#include <iostream>
#include <string>
#include <boost/spirit/include/classic_regex.hpp>
int
main(int args, char** argv)
{
boost::match_results<std::string::const_iterator> what;
if(args == 3) {
std::string text(argv[1]);
boost::regex expression(argv[2]);
std::cout << "Text : " << text << std::endl;
std::cout << "Regex: " << expression << std::endl;
if(boost::regex_match(text, what, expression, boost::match_default) != 0) {
int i = 0;
std::cout << text;
if(what[0].matched)
std::cout << " matches with regex pattern!" << std::endl;
else
std::cout << " does not match with regex pattern!" << std::endl;
for(boost::match_results<std::string::const_iterator>::const_iterator it=what.begin(); it!=what.end(); ++it) {
std::cout << "[" << (i++) << "] " << it->str() << std::endl;
}
} else {
std::cout << "Expression does not match!" << std::endl;
}
} else {
std::cout << "Usage: $> ./boost-regex <text> <regex>" << std::endl;
}
return 0;
}
如果我运行这些参数的程序,我没有得到期望的结果:
$> ./boost-regex "`cat file`" "(?=.*[^\t]).*"
Text : This text includes some tabulators
Regex: (?=.*[^\t]).*
This text includes some tabulators matches with regex pattern!
[0] This text includes some tabulators
在这种情况下,我会预计什么[0]。匹配的是错误的,但事实并非如此。
我的正则表达式有任何错误吗?
还是我必须使用其他格式/比赛标志?
预先感谢您!
您给程序的实际文本没有任何标签,就像您在输出中看到的那样(它显示文本“\ t”而不是打印实际标签)。 – 2012-03-05 12:08:41
这是正确的,我只想示范一个简短的例子!我正在使用包含标签的文本文件(使用hexdump进行验证 - > 0x09)。我纠正了我的例子! – janr 2012-03-05 12:19:55