2017-11-22 1 views
2

특수 형식 문자열의 구문을 구문 분석하는 규칙 표현을 작성하고 싶습니다. 형식 오류를 감지하고 형식 문자열을 처리 할 별도의 부분으로 분할하는 데 도움이됩니다.C++ 11 정규식이 그룹을 예상대로 반환하지 않습니다.

하지만 열심히 시도해 볼 때 예상대로 분할을 수행 할 수 없습니다.

'(?)'구문은 분할되지 않은 그룹을 정의해야하며 '()'는 별도로 반환되는 하위 일치를 정의해야합니다. 그러나 그렇지 않습니다. 여기

내 코드입니다 :

#include <iostream> 
#include <regex> 
#include <string> 

std::string parseCode(std::regex_constants::error_type etype); 

int main() 
{ 
    const std::string regex_str("(?:([^\\[]+)(\\[[^\\]]*\\])(+|\\n))"); 
    std::regex atr; 

    std::cout << "regex string = '" << regex_str << "'" << std::endl; 

    try 
    { 
     atr.assign(regex_str); 
    } catch (const std::regex_error& e) 
    { 
     std::cerr << "Error: " << e.what() << "; code: " << parseCode(e.code()) << std::endl; 
     exit(EXIT_FAILURE); 
    } // end try 

    { 
     const std::string title("First Title[] Second Title[] -Third Title[]"); 
     auto regex_begin = std::sregex_iterator(title.begin(), title.end(), atr); 

     for (std::sregex_iterator i = regex_begin; i != std::sregex_iterator(); ++i) 
     { 
     std::smatch match = *i; 
     std::cout << "got: '" << match.str() << "'" << std::endl; 
     } // end for 

     auto subregex_begin = std::sregex_token_iterator(title.begin(), 
     title.end(), atr, -1); 

     for (std::sregex_token_iterator i = subregex_begin; i != std::sregex_token_iterator(); ++i) 
     { 
     std::cout << "got sub: '" << *i << "'" << std::endl; 
     } // end for 
    } // end scope 

} 

std::string parseCode(std::regex_constants::error_type etype) 
{ 

    switch (etype) 
    { 
    case std::regex_constants::error_collate: 
     return "error_collate: invalid collating element request"; 
    case std::regex_constants::error_ctype: 
     return "error_ctype: invalid character class"; 
    case std::regex_constants::error_escape: 
     return "error_escape: invalid escape character or trailing escape"; 
    case std::regex_constants::error_backref: 
     return "error_backref: invalid back reference"; 
    case std::regex_constants::error_brack: 
     return "error_brack: mismatched bracket([ or ])"; 
    case std::regex_constants::error_paren: 
     return "error_paren: mismatched parentheses((or))"; 
    case std::regex_constants::error_brace: 
     return "error_brace: mismatched brace({ or })"; 
    case std::regex_constants::error_badbrace: 
     return "error_badbrace: invalid range inside a { }"; 
    case std::regex_constants::error_range: 
     return "erro_range: invalid character range(e.g., [z-a])"; 
    case std::regex_constants::error_space: 
     return "error_space: insufficient memory to handle this regular expression"; 
    case std::regex_constants::error_badrepeat: 
     return "error_badrepeat: a repetition character (*, ?, +, or {) was not preceded by a valid regular expression"; 
    case std::regex_constants::error_complexity: 
     return "error_complexity: the requested match is too complex"; 
    case std::regex_constants::error_stack: 
     return "error_stack: insufficient memory to evaluate a match"; 
    default: 
     return ""; 
    } 
} 

그리고 여기에 출력입니다 :

regex string = '(?:([^\[]+)(\[[^\]]*\])(+))' 
got: 'First Title[] ' 
got: 'Second Title[] ' 
got sub: '' 
got sub: '' 
got sub: '-Third Title[]' 

그리고 이것이 내가/원하는 무엇을 기대 : 나는 g ++ 5.3.1를 사용하고

regex string = '(?:([^\[]+)(\[[^\]]*\])(+))' 
got: 'First Title[] ' 
got: 'Second Title[] ' 
got: '-Third Title[]' 
got sub: 'First Title' 
got sub: '[]' 
got sub: ' ' 
got sub: 'Second Title' 
got sub: '[]' 
got sub: ' ' 
got sub: '-Third Title' 
got sub: '[]' 

RHEL 7.2.
IdeOne.com에서 g ++ 6.3과 동일한 결과가 나타납니다. https://ideone.com/dj4Mqf

무엇이 잘못 되었나요? ,

전체 일치하는 문자열을 반환 match.str()
const std::string regex_str("([^\\[]+)(\\[[^\\]]*\\])(\\s+|\\n|$)"); 

2), 일치 추출하는 그룹operator[]을 사용합니다 :

답변

3

1) 귀하의 정규식 마지막 부분 일치하지 않는,로 변경

std::smatch match = *i; 
    std::cout << "got: 1='" << match[1] << "' 2='" << match[2] << "' 3='" << match[3] << "'" << std::endl; 

출력 :

regex string = '([^\[]+)(\[[^\]]*\])(\s+|\n|$)' 
got: 1='First Title' 2='[]' 3=' ' 
got: 1='Second Title' 2='[]' 3=' ' 
got: 1='-Third Title' 2='[]' 3='' 
관련 문제