C++ regex_match not working
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
Regex not working as expected with C++ regex_match
You need to use a regex_search
rather than regex_match
:
bool found = regex_search("<html>",regex("h.*l"));
See IDEONE demo
In plain words, regex_search
will search for a substring at any position in the given string. regex_match
will only return true if the entire input string is matched (same behavior as matches
in Java).
The regex_match documentation says:
Returns whether the target sequence matches the regular expression
rgx
.
The entire target sequence must match the regular expression for this function >to return true (i.e., without any additional characters before or after the >match). For a function that returns true when the match is only part of the >sequence, seeregex_search
.
The regex_search is different:
Returns whether some sub-sequence in the target sequence (the subject) matches the regular expression
rgx
(the pattern).
Complicated regex_match not working in C++
Thanks to @MYGz I was able to fix this. Maybe it had something to do with escape characters.
All I did to change @MYGz's suggestion was to remove the \s.
It worked with ([0-1]?[0-2]:[0-5][0-9] *[AaPp][Mm])
Regex is not working in C
grep -E
uses some enhanced ERE syntax meaning that the {n,m}
quantifier braces (and also (
and )
) do not have to be escaped (not the case in BRE regex).
You need to pass REG_EXTENDED
flag to the regcomp
, and also, since you can't use a word boundary, replace the first \b
with (^|[^[:alnum:]_])
"equivalent". You need no trailing \b
since there is a :
in the pattern right after:
const char *str_regex = "(^|[^[:alnum:]_])(abc|def):[0-9]{10}@([A-Za-z0-9].*)";
The (^|[^[:alnum:]_])
part matches either the start of the string (^
) or (|
) a char other than alphanumeric or an underscore.
Full C demo:
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
int main (void)
{
int match;
int err;
regex_t preg;
regmatch_t pmatch[4];
size_t nmatch = 4;
const char *str_request = "abc:1234567890@werty.wer.sdfg.net";
const char *str_regex = "(^|[^[:alnum:]_])(abc|def):[0-9]{10}@([A-Za-z0-9].*)";
err = regcomp(&preg, str_regex, REG_EXTENDED);
if (err == 0)
{
match = regexec(&preg, str_request, nmatch, pmatch, 0);
nmatch = preg.re_nsub;
regfree(&preg);
if (match == 0)
{
printf("\"%.*s\"\n", pmatch[2].rm_eo - pmatch[2].rm_so, &str_request[pmatch[2].rm_so]);
printf("\"%.*s\"\n", pmatch[3].rm_eo - pmatch[3].rm_so, &str_request[pmatch[3].rm_so]);
}
else if (match == REG_NOMATCH)
{
printf("unmatch\n");
}
}
return 0;
}
linux g++ regular expressions not working
you do not need '\' before ':'. so the correct line is:std::regex re("SerialNumber: (.*)\n");
with this change your code works with gcc starting gcc 4.9.1. https://wandbox.org/permlink/uuVCxdtpHAjZTghP
Why doesn't std::regex_match return true with $ pattern?
Note that regex_match will only successfully match a regular expression to an entire character sequence, whereas std::regex_search will successfully match subsequences.
You want regex_search
for matching subsequences. $
does not match the entirety of a non-empty string, but does match a (zero-length) substring of a non-empty string.
Related Topics
C++ Dynamic Array Initialization with Declaration
Why C++ Doesn't Support Named Parameter
C++ Array Size Dependent on Function Parameter Causes Compile Errors
Exporting Static Data in a Dll
Clion C++ Can't Read/Open .Txt File in Project Directory
Does Gcc Inline C++ Functions Without the 'Inline' Keyword
Should "Delete This" Be Called from Within a Member Method
How Concatenate a String and a Const Char
Preventing Gcc from Automatically Using Avx and Fma Instructions When Compiled with -Mavx and -Mfma
Set the Digits After Decimal Point
Reading Directly from an Std::Istream into an Std::String
Mingw Error: 'Thread' Is Not a Member of 'Std'
How to Easily Indent Output to Ofstream
Will Consteval Allow Using Static_Assert on Function Arguments
What Does the & (Ampersand) at the End of Member Function Signature Mean
Any Good Solutions for C++ String Code Point and Code Unit
How to Implement Tesseract to Run with Project in Visual Studio 2010