C++11 regex doesn't match the string
You were specifying POSIX basic regex, in that format you must escape ()
and {}
I was able to get get matches with a few changes:
int main(int argc, const char * argv[]){
using std::cout;
using std::endl;
std::regex regexp(R"(\([0-9]\{16\}\):\([0-9]\{5,20\}\):\([a-zA-Z0-9\\+/=]\{28\}\))",std::regex_constants::basic);
std::smatch match;
std::string stringified = "1111111111111111:1384537090:Gl21j08WWBDUCmzq9JZoOXDzzP8=";
if (std::regex_search(stringified, match, regexp)) {
cout << match[1] << "," << match[2] << "," << match[3]<< endl;
} else {
cout << "No matches found" << endl;
}
return 0;
}
Or you could use:
std::regex_constants::extended
If you use std::regex_constants::extended
you should not escape ()
and {}
If you don't want to use a raw string, you can do that as well:
std::regex regexp("([0-9]{16}):([0-9]{5,20}):([a-zA-Z0-9\\\\+/=]{28})",std::regex_constants::extended);
You'll just have to double up on the \\
to properly escape them. The above regex also works with the default regex grammar std::regex_constants::ECMAScript
std::regex regexp("([0-9]{16}):([0-9]{5,20}):([a-zA-Z0-9\\\\+/=]{28})");
It looks like GCC just added regex supported in their development branch of GCC 4.9.
C++11 Regex Matching
See gcc's stdc++11 implementation status page -- regexes are not supported as of gcc 4.8
Edit for posterity: As mentioned in the comments, the regex library is now in libstdc++
and should be in gcc 4.9 and on.
C++ regex match, not matching
The regex_match fails when the string doesnt match EXACTLY the pattern. Note that the brd ff:ff:ff:ff:ff:ff
part of the string isnt being matched. All you need to do, then, is to append a .* to the pattern:
^\\d{1}:\\s+(\\w+).*?link\\/ether\\s{1}([a-z0-9:]+).*
Also, for that example, the loop isnt necessary. You can use:
if (std::regex_match(line, pieces, interface_address)) {
std::string name = pieces[1];
std::string address = pieces[2];
std::cout << name << address << std::endl;
}
Why doesn't this regex match?
There are two issues with your code, both related to invalid escape-sequences:
"\d"
is interpreted as an escape-sequence, the resulting string (passed tostd::regex
) will not contain what you expect — instead use"\\d"
to properly get a slash followed by the letterd
."\/"
is not a valid escape-sequence, and there really is no need for you to escape/
, instead leave it as if ("/"
).
Working Example
#include <regex>
#include <iostream>
#include <string>
int main () {
bool result = std::regex_match (
std::string ("f 1/1/1 3/3/1 4/4/1"),
std::regex ("f \\d+/\\d+/\\d+ \\d+/\\d+/\\d+ \\d+/\\d+/\\d+")
);
std::cout << result << std::endl; // 1
}
How to use regex in C++11
If you are compiling with g++ it may be because regex is not fully supported yet. See here for current C++11 status in g++.
C++11 regex matching a full word that does not end with a period?
You need to make sure the word is followed with a word boundary:
std::regex rex(R"(\w+\b(?!\.))");
See the regex demo
Otherwise, backtracking occurs and you find jo
in joe.
with your pattern.
I also advise to use raw string literals when defining a regex, you get rid of excessive backslashes this way.
Conditionally ignore case in c++11 regular expressions
There are many ways to conditionally set the flag. For example using the conditional operator:
std::regex r(match, ic ? std::regex_constants::icase | std::regex_constants::collate
: std::regex_constants::collate);
C++11: Safe practice with regex of two possible number of matches
m.size()
will always be the number of marked subexpressions in your expression plus 1 (for the whole expression).
In your code you have 4 marked subexpressions, whether these are matched or not has no effect on the size of m
.
If you want to now if there are milliseconds, you can check:
m[4].matched
Related Topics
Undefined Reference to '_Gxx_Personality_Sj0'
More Elegant Way to Check for Duplicates in C++ Array
Workaround for Template Argument Deduction in Non-Deduced Context
How to Read a Growing Text File in C++
Order of Execution in Constructor Initialization List
Check If a Variable Type Is Iterable
Unordered_Map Hash Function C++
How to Hook Windows Functions in C/C++
Error: Invalid Initialization of Non-Const Reference of Type 'Int&' from an Rvalue of Type 'Int'
Variable Declarations in Header Files - Static or Not
Explain C++ Sfinae to a Non-C++ Programmer
Is a Return Statement Mandatory for C++ Functions That Do Not Return Void
False Positive with Is_Copy_Constructible on Vector<Unique_Ptr>