简体   繁体   English

Boost正则表达式与标签不匹配

[英]Boost regex don't match tabs

I'm using boost regex_match and I have a problem with matching no tab characters. 我正在使用boost regex_match,我遇到了匹配没有制表符的问题。 My test application looks as follows: 我的测试应用程序如下所示:

#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;
}

If I run the program with these arguments, I don't get the expected result: 如果我用这些参数运行程序,我得不到预期的结果:

$> ./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

In this case I would have expected that what[0].matched is false, but it's not. 在这种情况下,我会期望[0] .matched是假的,但事实并非如此。

Is there any mistake in my regular expression? 我的正则表达式中有错误吗?
Or do I have to use other format/match flag? 或者我必须使用其他格式/匹配标志?

Thank you in advance! 先感谢您!

I am not sure what you want to do. 我不确定你想做什么。 My understanding is, you want the regex to fail as soon as there is a tab in the text. 我的理解是,一旦文本中有选项卡,您希望正则表达式失败。

Your positive lookahead assertion (?=.*[^\\t]) is true as soon as it finds a non tab, and there are a lot of non tabs in your text. 一旦找到非选项卡,你的正向前瞻断言(?=.*[^\\t])为真,你的文本中有很多非选项卡。

If you want it to fail, when there is a tab, go the other way round and use a negative lookahead assertion. 如果你想让它失败,当有一个标签时,反过来使用负前瞻断言。

(?!.*\t).*

this assertion will fail as soon as it find a tab. 一旦找到标签,这个断言就会失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM