簡體   English   中英

Boost正則表達式與標簽不匹配

[英]Boost regex don't match tabs

我正在使用boost regex_match,我遇到了匹配沒有制表符的問題。 我的測試應用程序如下所示:

#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] .matched是假的,但事實並非如此。

我的正則表達式中有錯誤嗎?
或者我必須使用其他格式/匹配標志?

先感謝您!

我不確定你想做什么。 我的理解是,一旦文本中有選項卡,您希望正則表達式失敗。

一旦找到非選項卡,你的正向前瞻斷言(?=.*[^\\t])為真,你的文本中有很多非選項卡。

如果你想讓它失敗,當有一個標簽時,反過來使用負前瞻斷言。

(?!.*\t).*

一旦找到標簽,這個斷言就會失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM