이 C ++ 11 정규식 오류 나 또는 컴파일러입니까?
좋아, 이것은 내가이 문제가 있었던 원래 프로그램이 아니지만 훨씬 작은 프로그램에 복제했습니다. 아주 간단한 문제입니다.
main.cpp :
#include <iostream>
#include <regex>
using namespace std;
int main()
{
regex r1("S");
printf("S works.\n");
regex r2(".");
printf(". works.\n");
regex r3(".+");
printf(".+ works.\n");
regex r4("[0-9]");
printf("[0-9] works.\n");
return 0;
}
이 명령으로 성공적으로 컴파일되었으며 오류 메시지가 없습니다.
$ g++ -std=c++0x main.cpp
참고로의 마지막 줄 g++ -v은 다음과 같습니다.
gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3)
그리고 내가 그것을 실행하려고 할 때의 결과 :
$ ./a.out
S works.
. works.
.+ works.
terminate called after throwing an instance of 'std::regex_error'
what(): regex_error
Aborted
내가 R4로 변경하면이 같은 방식으로 발생 \\s, \\w또는를 [a-z]. 컴파일러에 문제가 있습니까? C ++ 11의 정규식 엔진이 "공백"또는 "단어 문자"를 다른 방식으로 표현한다고 믿을 수 있지만 대괄호가 작동하지 않는 것은 확장입니다. 4.6.2에서 수정 된 것이 있습니까?
편집하다:
요아킴 Pileborg은 추가 사용, 부분적인 솔루션을 공급하고 regex_constants지원 대괄호가 있지만, 어느 쪽도 아니하는 구문을 사용하려면 매개 변수를 basic, extended, awk, 나 ECMAScript처럼 지원 슬래시 이스케이프 조건에 보이지 않는다 \\s, \\w또는 \\t.
편집 2 :
( R"(\w)"대신 "\\w") 원시 문자열을 사용 하는 것도 작동하지 않는 것 같습니다.
업데이트 : <regex>이제 GCC 4.9.0에서 구현 및 출시되었습니다.
이전 답변 :
ECMAScript를 구문 받아 [0-9], \s, \w, 등을 참조 ECMA-262 (15.10) . 다음 boost::regex은 기본적으로 ECMAScript 구문도 사용 하는 예입니다 .
#include <boost/regex.hpp>
int main(int argc, char* argv[]) {
using namespace boost;
regex e("[0-9]");
return argc > 1 ? !regex_match(argv[1], e) : 2;
}
효과가있다:
$ g++ -std=c++0x *.cc -lboost_regex && ./a.out 1
C ++ 11 표준 (28.8.2)에 따르면 기본적으로 플래그를 basic_regex()사용 하므로이 regex_constants::ECMAScript구문을 이해해야합니다.
Is this C++11 regex error me or the compiler?
gcc-4.6.1 doesn't support c++11 regular expressions (28.13).
The error is because creating a regex by default uses ECMAScript syntax for the expression, which doesn't support brackets. You should declare the expression with the basic or extended flag:
std::regex r4("[0-9]", std::regex_constants::basic);
Edit Seems like libstdc++ (part of GCC, and the library that handles all C++ stuff) doesn't fully implement regular expressions yet. In their status document they say that Modified ECMAScript regular expression grammar is not implemented yet.
Regex support improved between gcc 4.8.2 and 4.9.2. For example, the regex =[A-Z]{3} was failing for me with:
Regex error
After upgrading to gcc 4.9.2, it works as expected.
ReferenceURL : https://stackoverflow.com/questions/8060025/is-this-c11-regex-error-me-or-the-compiler
'programing' 카테고리의 다른 글
| 자바 스레드 재사용 (0) | 2021.01.15 |
|---|---|
| 빌더 패턴 및 다수의 필수 매개 변수 (0) | 2021.01.15 |
| stderr 및 디버그에 오류 로깅, log4j를 사용하여 stdout에 정보 로깅 (0) | 2021.01.15 |
| Haskell의 ($)는 마술 연산자입니까? (0) | 2021.01.15 |
| 나는“이혼”을 겪고 있습니까? (0) | 2021.01.15 |