Could someone explain the differences between the two? Which would be better to learn right now? How would knowledge transfer from one to the other and vice-versa?
-
5Learning the standard way is never a bad thing. – Mark Ransom Sep 28 '11 at 21:37
-
4Be careful with the Standard Library C++11 Regex, because it is still not fully implemented by GCC: http://stackoverflow.com/questions/4716680/c0x-regex-in-gcc – Mihai Todor Aug 01 '12 at 22:26
3 Answers
The boost regex library made it into C++0x so I'm guessing it will slowly be removed from boost. However, using boost is nice because you can still use it with compilers without C++0x support. So it's really up to you.

- 18,070
- 14
- 92
- 160
-
3Yes, but can one migrate a Boost.Regex-based program to C++11 Regex with a simple `s/boost::/std::/g` ? – Robᵩ Sep 28 '11 at 21:22
-
@Rob : I imagine that depends on whether you're using Boost.Regex's ICU support. – ildjarn Sep 28 '11 at 21:25
-
@Robᵩ There are some minor changes that make the two implementations not a drop-in replacement for the other. e.g. https://stackoverflow.com/questions/37829849/different-treatment-of-in-regex-replace-replacement-string-in-g-and-boos – luca Dec 16 '21 at 15:10
One major difference is, that C++11 does not provide the Perl syntax for regular expressions. So, if you tend to use Perl syntax you have to use the Boost::Regex library.

- 81
- 1
- 1
-
Yes there is Perl syntax for regex : There is ECMAScript regex and ECMAScript regex is the same as Perl regex http://ecma-international.org/ecma-262/5.1/#sec-15.10 – BenjaminB Aug 01 '12 at 22:16
-
5@Ubiquité you are not correct. The link you provided says "modeled after". The [C++ TR1 proposal for
](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2003/n1429.htm#syntax_discussion) uses the very similar phrasing "based on". ECMAScript regular expressions are not the same thing as Perl regular expressions -- Perl provides more features and some different syntax. – Patrick Niedzielski Aug 07 '12 at 23:26 -
1@PatrickNiedzielski: You are correct, unfortunately Boost.Regex defined `perl` as equivalent to `ECMAScript` http://www.boost.org/doc/libs/1_53_0/libs/regex/doc/html/boost_regex/ref/syntax_option_type/syntax_option_type_perl.html. – kennytm Jun 19 '13 at 20:48
-
@kennytm does that mean boost "perl" regex syntax and C++11 `ECMAScript` regex _are_ the same? – Mahmoud Al-Qudsi Aug 17 '17 at 19:54
-
@MahmoudAl-Qudsi. Yes. However, as of 1.64.0, boost's "ECMAScript" regex actually [implements a lot of Perl-but-not-ECMAScript regex features](http://www.boost.org/doc/libs/1_64_0/libs/regex/doc/html/boost_regex/syntax/perl_syntax.html). – kennytm Aug 17 '17 at 20:08
-
-
-
@kennytm if you read the bottom of that boost doc, it says: "The options normal, ECMAScript, JavaScript and JScript are all synonyms for perl." – Orwellophile Nov 04 '17 at 20:53
-
@Orwellophile that just means that they don't distinguish the flavours in their implementation (you get everything enabled always) – sehe Jun 20 '20 at 22:33
At least in Visual Studio 2013 this and related names (cmatch, regex_match) are the same in both namespaces. They also have the same (or similar?) interface.
So you can just change namespace and the same code will be compiled with another regex without warning and errors. And it should work the same of course.
P.S. I would prefer std::regex since it is part of C++11 and boost::regex is a third-party library. I'm sure few years later, boost will remove support for boost::regex.

- 1,552
- 20
- 18
-
1There are some minor chages that make the two implementations not a drop-in replacement for the other. e.g. https://stackoverflow.com/questions/37829849/different-treatment-of-in-regex-replace-replacement-string-in-g-and-boos – luca Dec 16 '21 at 15:08
-
1And I would not expect removing regex from boost even if they were drop-in replacements for each other. Those expectation was too adventurous. – Sergey Dec 22 '21 at 15:33