std::regex_match outputs false when it should output true [duplicate]

3 weeks ago 23
ARTICLE AD BOX

The problem is that std::regex_match must match the entire string but you are trying to match only part of it.

You need to either use std::regex_search or alter your regular expression to match all three parts at once:

#include <regex> #include <string> #include <iostream> const auto test = { "foo.bar = 15" , "baz.asd = 13" , "ddd.dgh = 66" }; int main() { const std::regex r(R"~(([^.]+)\.([^\s]+)[^0-9]+(\d+))~"); // ( 1 ) ( 2 ) ( 3 ) <- capture groups std::cmatch m; for(const auto& line: test) { if(std::regex_match(line, m, r)) { // m.str(0) is the entire matched string // m.str(1) is the 1st capture group // etc... std::cout << "a = " << m.str(1) << '\n'; std::cout << "b = " << m.str(2) << '\n'; std::cout << "c = " << m.str(3) << '\n'; std::cout << '\n'; } } }

Regular expression: https://regex101.com/r/kB2cX3/2

Output:

a = foo b = bar c = 15 a = baz b = asd c = 13 a = ddd b = dgh c = 66

answered May 25, 2015 at 20:04

Galik's user avatar

To focus on regex patterns I'd prefer to use raw string literals in c++:

regex cvarPattern ( R"rgx(\.([a-zA-Z_]+))rgx" ); regex parentPattern ( R"rgx(^([a-zA-Z0-9_]+)\.)rgx" ); regex cvarValue ( R"rgx(\.[a-zA-Z0-9_]+[ ]*=[ ]*(\d+\.*\d*))rgx" );

Everything between the rgx( )rgx delimiters doesn't need any extra escaping for c++ char literal characters.


Actually what you have written in your question resembles to those regular expressions I've been writing as raw string literals.
You probably simply meant something like

regex cvarPattern ( R"rgx(.([a-zA-Z_]+))rgx" ); regex parentPattern ( R"rgx(^([a-zA-Z0-9_]+).)rgx" ); regex cvarValue ( R"rgx(.[a-zA-Z0-9_]+[ ]*=[ ]*(\d+(\.\d*)?))rgx" );

I didn't dig in deeper, but I'm not getting all of these escaped characters in your regular expression patterns now.


As for your question in the comment, you can use a choice of matching sub-pattern groups, and check for which of them was applied in the matches structure:

regex cvarValue ( R"rgx(.[a-zA-Z0-9_]+[ ]*=[ ]*((\d+)|(\d+\.\d?)|([a-zA-Z]+)){1})rgx" ); // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

You probably don't need these cvarPattern and parentPattern regular expressions to inspect other (more detailed) views about the matching pattern.

answered May 25, 2015 at 19:57

πάντα ῥεῖ's user avatar

2 Comments

I added \. in my regex pattern, because there also may be float values, such as 2.5, and I probably made some mistakes, because I edited it 9000 times

2015-05-25T20:17:47.097Z+00:00

@mlgpro You'll need to have a choice of groups like (\d+)|(\d+\.\d?)|([a-zA-Z]+) or so.

2015-05-25T20:21:24.367Z+00:00

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.

Read Entire Article