2012-03-05 2 views
0

부스트 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] .match은 거짓이지만 그렇지 않습니다.

정규 표현식에 실수가 있습니까?
다른 형식/일치 플래그를 사용해야합니까?

미리 감사드립니다.

+4

출력에 표시되는 것처럼 실제 프로그램에주는 텍스트에는 탭이 없습니다 (실제 탭을 인쇄하는 대신 "\ t"텍스트가 표시됨). –

+0

이것이 맞습니다. 저는 간단한 예를 보여주고 싶었습니다! 탭 (hexdump -> 0x09로 검증 됨)을 포함한 텍스트 파일을 사용하고 있습니다. 나는 나의 모범을 교정했다! – janr

답변

2

당신이하고 싶은 것이 확실하지 않습니다. 내 이해, 텍스트에 탭이있는 즉시 정규식 실패 싶습니다.

긍정적 인 미리보기 주장 (?=.*[^\t])은 탭이 아닌 것을 알게되고 텍스트에 많은 탭이없는 한 true입니다.

실패한 경우 탭이있는 경우 반대쪽으로 이동하여 부정적인 미리보기 주장을 사용하십시오.

(?!.*\t).* 

이 어설 션은 탭을 찾으면 바로 실패합니다.

+0

이 작품, 고마워요! – janr

관련 문제