Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
293 views
in Technique[技术] by (71.8m points)

c++ - Is gcc 4.8 or earlier buggy about regular expressions?

I am trying to use std::regex in a C++11 piece of code, but it appears that the support is a bit buggy. An example:

#include <regex>
#include <iostream>

int main (int argc, const char * argv[]) {
    std::regex r("st|mt|tr");
    std::cerr << "st|mt|tr" << " matches st? " << std::regex_match("st", r) << std::endl;
    std::cerr << "st|mt|tr" << " matches mt? " << std::regex_match("mt", r) << std::endl;
    std::cerr << "st|mt|tr" << " matches tr? " << std::regex_match("tr", r) << std::endl;
}

outputs:

st|mt|tr matches st? 1
st|mt|tr matches mt? 1
st|mt|tr matches tr? 0

when compiled with gcc (MacPorts gcc47 4.7.1_2) 4.7.1, either with

g++ *.cc -o test -std=c++11
g++ *.cc -o test -std=c++0x

or

g++ *.cc -o test -std=gnu++0x

Besides, the regex works well if I only have two alternative patterns, e.g. st|mt, so it looks like the last one is not matched for some reasons. The code works well with the Apple LLVM compiler.

Any ideas about how to solve the issue?

Update one possible solution is to use groups to implement multiple alternatives, e.g. (st|mt)|tr.

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...