首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Boost正则表达式不匹配选项卡

Boost正则表达式不匹配选项卡
EN

Stack Overflow用户
提问于 2012-03-05 11:59:46
回答 1查看 783关注 0票数 0

我使用boost regex_match,在匹配没有选项卡字符时遇到了问题。我的测试应用程序如下所示:

代码语言:javascript
复制
#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;
}

如果我使用这些参数运行程序,就不会得到预期的结果:

代码语言:javascript
复制
$> ./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

在这种情况下,我本以为what.matched是假的,但事实并非如此。

我的正则表达式有什么错误吗?

还是必须使用其他格式/匹配标志?

提前谢谢你!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-05 12:54:46

我不知道你想做什么。我的理解是,只要文本中有一个选项卡,您就希望regex失败。

您的积极展望断言(?=.*[^\t])在找到非选项卡后立即为真,并且文本中有许多非选项卡。

如果您希望它失败,当有一个选项卡时,转到相反的方向,并使用一个负的前瞻性断言。

代码语言:javascript
复制
(?!.*\t).*

一旦找到选项卡,这个断言就会失败。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9566257

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档