2012-09-21 12:13:43 8 Comments
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
.
Related Questions
Sponsored Content
19 Answered Questions
[SOLVED] How do you use a variable in a regular expression?
- 2009-01-30 00:11:05
- JC Grubbs
- 686590 View
- 1256 Score
- 19 Answer
- Tags: javascript regex
73 Answered Questions
[SOLVED] How to validate an email address using a regular expression?
- 2008-10-14 14:14:34
- acrosman
- 1168810 View
- 3210 Score
- 73 Answer
- Tags: regex validation email email-validation string-parsing
52 Answered Questions
[SOLVED] What is the best regular expression to check if a string is a valid URL?
- 2008-10-02 10:53:17
- Vitor Silva
- 378651 View
- 754 Score
- 52 Answer
- Tags: regex url language-agnostic
29 Answered Questions
[SOLVED] Regular expression to match a line that doesn't contain a word
- 2009-01-02 07:30:16
- knaser
- 3166603 View
- 4131 Score
- 29 Answer
- Tags: regex regex-negation
12 Answered Questions
10 Answered Questions
[SOLVED] jQuery selector regular expressions
- 2008-10-10 05:40:48
- Joel Cunningham
- 405871 View
- 565 Score
- 10 Answer
- Tags: javascript jquery regex jquery-selectors
18 Answered Questions
[SOLVED] How do you access the matched groups in a JavaScript regular expression?
- 2009-01-11 07:21:20
- nickf
- 722073 View
- 1279 Score
- 18 Answer
- Tags: javascript regex
8 Answered Questions
[SOLVED] Is there a regular expression to detect a valid regular expression?
- 2008-10-05 17:07:35
- psytek
- 199427 View
- 970 Score
- 8 Answer
- Tags: regex
15 Answered Questions
[SOLVED] What is a non-capturing group in regular expressions?
- 2010-08-18 13:17:47
- never_had_a_name
- 665469 View
- 1660 Score
- 15 Answer
- Tags: regex capturing-group regex-group
17 Answered Questions
[SOLVED] Regular Expression for alphanumeric and underscores
- 2008-12-03 04:25:27
- Jim
- 1033355 View
- 548 Score
- 17 Answer
- Tags: regex
3 comments
@Luis Orantes 2017-07-10 18:25:53
At this moment (using std=c++14 in g++ (GCC) 4.9.2) is still not accepting regex_match.
Here is an approach that works like regex_match but using sregex_token_iterator instead. And it works with g++.
it will print 1 2 3
you may read the sregex_token_iterator reference in: http://en.cppreference.com/w/cpp/regex/regex_token_iterator
@Jonathan Wakely 2017-09-29 11:45:35
"At this moment (using std=c++14 in g++ (GCC) 4.9.2) is still not accepting regex_match." That's not true, you're probably using it wrong.
@Jonathan Wakely 2017-09-29 11:58:37
Your code is not "an approach that works like regex_match" because that function tries to match sub-strings, not the entire string, so I still think you're using it wrong. You can do it with
std::regex_search
though, see wandbox.org/permlink/rLbGyYcYGNsBWsaB@Matt Clarkson 2016-12-16 14:07:54
Feature Detection
This is a snippet to detect if the
libstdc++
implementation is implemented with C preprocessor defines:Macros
_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT
is defined inbits/regex.tcc
in4.9.x
_GLIBCXX_REGEX_STATE_LIMIT
is defined inbits/regex_automatron.h
in5+
_GLIBCXX_RELEASE
was added to7+
as a result of this answer and is the GCC major versionTesting
You can test it with GCC like this:
Results
Here are some results for various compilers:
Here be Dragons
This is totally unsupported and relies on the detection of private macros that the GCC developers have put into the
bits/regex*
headers. They could change and go away at anytime. Hopefully, they won't be removed in the current 4.9.x, 5.x, 6.x releases but they could go away in the 7.x releases.If the GCC developers added a
#define _GLIBCXX_HAVE_WORKING_REGEX 1
(or something, hint hint nudge nudge) in the 7.x release that persisted, this snippet could be updated to include that and later GCC releases would work with the snippet above.As far as I know, all other compilers have a working
<regex>
when__cplusplus >= 201103L
but YMMV.Obviously this would completely break if someone defined the
_GLIBCXX_REGEX_DFS_QUANTIFIERS_LIMIT
or_GLIBCXX_REGEX_STATE_LIMIT
macros outside of thestdc++-v3
headers.@Jonathan Wakely 2016-12-16 18:06:56
Very nice! I was going to suggest checking for the header guard macro from one of the headers that is new in GCC 4.9, but they don't have guards :-\ The macros aren't changing for GCC 7, but theoretically they could do for GCC 8+, so please file an enhancement request at gcc.gnu.org/bugzilla asking for something like
_GLIBCXX_REGEX_IS_OK_NOW_KTHXBAI
in the headers, so it doesn't get forgotten - thanks!@Matt Clarkson 2016-12-22 17:40:23
@JonathanWakely have added 78905. I'm not sure how to make that into an enhancement bug but it's in the system now.
@Jonathan Wakely 2012-09-30 21:45:38
<regex>
was implemented and released in GCC 4.9.0.In your (older) version of GCC, it is not implemented.
That prototype
<regex>
code was added when all of GCC's C++0x support was highly experimental, tracking early C++0x drafts and being made available for people to experiment with. That allowed people to find problems and give feedback to the standard committee before the standard was finalised. At the time lots of people were grateful to have had access to bleeding edge features long before C++11 was finished and before many other compilers provided any support, and that feedback really helped improve C++11. This was a Good ThingTM.The
<regex>
code was never in a useful state, but was added as a work-in-progress like many other bits of code at the time. It was checked in and made available for others to collaborate on if they wanted to, with the intention that it would be finished eventually.That's often how open source works: Release early, release often -- unfortunately in the case of
<regex>
we only got the early part right and not the often part that would have finished the implementation.Most parts of the library were more complete and are now almost fully implemented, but
<regex>
hadn't been, so it stayed in the same unfinished state since it was added.It wasn't such a bad idea a few years ago, when C++0x was still a work in progress and we shipped lots of partial implementations. No-one thought it would remain unusable for so long so, with hindsight, maybe it should have been disabled and required a macro or built-time option to enable it. But that ship sailed long ago. There are exported symbols from the libstdc++.so library that depend on the regex code, so simply removing it (in, say, GCC 4.8) would not have been trivial.