简体   繁体   中英

std::regex::multiline does not exist

I try to use multiline regex syntax, so that ^$ match beginning and end of line in C++. Since C++17, std::regex and std::regex_constants should contain a flag multiline that turns this on ( see the reference ).

I've tried this on Apple Clang, GCC on Ubuntu and Alpine. In none of those it works, the symbol is said to be missing. What am I doing wrong? Did I misunderstand this is in the standard since C++17?

#include <regex>
int main() {
  auto flag = std::regex::multiline;
}
$ g++ --version
g++ (Ubuntu 9.2.1-9ubuntu2) 9.2.1 20191008
$ g++ regex.cpp -std=c++17 -o regex
regex.cpp: In function 'int main()':
regex.cpp:3:27: error: 'multiline' is not a member of 'std::__cxx11::regex' {aka 'std::__cxx11::basic_regex<char>'}
    3 |   auto flag = std::regex::multiline;
      |                           ^~~~~~~~~

It's a big limitation of std::regex . The only good solution I found was to use boost::regex instead, and then you can do multilines just fine, and the nice thing is that it's exactly the same syntax, you just need to replace all your std:: with boost:: . Oh, and it also runs about 20x faster.

Have a look at the comparison table here: https://www.regular-expressions.info/boost.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM