parsing/matching string occurrence in C - c

I have the following string:
const char *str = "\"This is just some random text\" 130 28194 \"Some other string\" \"String 3\""
I would like to get the the integer 28194 of course the integer varies, so I can't do strstr("20194").
So I was wondering what would be a good way to get that part of the string?
I was thinking to use #include <regex.h> which I already have a procedure to match regexp's but not sure how the regexp in C will look like using the POSIX style notation. [:alpha:]+[:digit:] and if performance will be an issue. Or will it be better using strchr,strstr?
Any ideas will be appreciate it

If you want to use regex, you can use:
const char *str = "\"This is just some random text\" 130 28194 \"Some other string\" \"String 3\"";
regex_t re;
regmatch_t matches[2];
int comp_ret = regcomp(&re, "([[:digit:]]+) \"", REG_EXTENDED);
if(comp_ret)
{
// Error occured. See regex.h
}
if(!regexec(&re, str, 2, matches, 0))
{
long long result = strtoll(str + matches[1].rm_so, NULL, 10);
printf("%lld\n", result);
}
else
{
// Didn't match
}
regfree(&re);
You're correct that there are other approaches.
EDIT: Changed to use non-optional repetition and show more error checking.

Related

Can't use regular expression with .*

I've been trying to use regular expressions (<regex.h>) in a C project I am developing.
According to regex101 the regex it is well written and identifies what I'm trying to identify but it doesn't work when I try to run it in C.
#include <stdio.h>
#include <regex.h>
int main() {
char pattern[] = "#include.*";
char line[] = "#include <stdio.h>";
regex_t string;
int regex_return = -1;
regex_return = regcomp(&string, line, 0);
regex_return += regexec(&string, pattern, 0, NULL, 0);
printf("%d", regex_return);
return 0;
}
This is a sample code I wrote to test the expression when I found out it didn't work.
It prints 1, when I expected 0.
It prints 0 if I change the line to "#include", which is just strange to me, because it's ignoring the .* at the end.
line and pattern are swapped.
regcomp takes the pattern and regexec takes the string to check.

C regular expression to validate hostname, right expresion but wrong output [duplicate]

I'd like to match all strings that begin with a set of characters a-z, then exactly one : and another set of characters a-z right after that.
So as an example, the string "an:example" would be a correct match.
And another example, "another:ex:ample" needs to be a mismatch.
I have tried to set it up like that but it matches everything, even if i take bad string as input :(
So my regular expression is "[a-z]:[a-z]" but it evaluates the string "1an:example" as a Match :/
How can I do this correctly?
#include <stdio.h>
#include <regex.h>
int main() {
regex_t regex;
int retis;
char* str = "1an:example";
retis = regcomp(&regex, "[a-z]:[a-z]", 0);
retis = regexec(&regex, str, 0, NULL, 0);
if(!retis) {
puts("Match");
}
else if(retis == REG_NOMATCH) {
puts("No match");
}
regfree(&regex);
return 0;
}
You need
retis = regcomp(&regex, "^[a-z]+:[a-z]+$", REG_EXTENDED);
See the C online demo.
That is:
^ (start of string) and $ (end of string) are anchors that require the regex to match the whole string
[a-z]+ matches one or more lowercase letters
REG_EXTENDED allows extended regex syntax, e.g. in regex.h it is required to enable the $ anchor.

Issue with setting a correct regular expression in C using regex

I'd like to match all strings that begin with a set of characters a-z, then exactly one : and another set of characters a-z right after that.
So as an example, the string "an:example" would be a correct match.
And another example, "another:ex:ample" needs to be a mismatch.
I have tried to set it up like that but it matches everything, even if i take bad string as input :(
So my regular expression is "[a-z]:[a-z]" but it evaluates the string "1an:example" as a Match :/
How can I do this correctly?
#include <stdio.h>
#include <regex.h>
int main() {
regex_t regex;
int retis;
char* str = "1an:example";
retis = regcomp(&regex, "[a-z]:[a-z]", 0);
retis = regexec(&regex, str, 0, NULL, 0);
if(!retis) {
puts("Match");
}
else if(retis == REG_NOMATCH) {
puts("No match");
}
regfree(&regex);
return 0;
}
You need
retis = regcomp(&regex, "^[a-z]+:[a-z]+$", REG_EXTENDED);
See the C online demo.
That is:
^ (start of string) and $ (end of string) are anchors that require the regex to match the whole string
[a-z]+ matches one or more lowercase letters
REG_EXTENDED allows extended regex syntax, e.g. in regex.h it is required to enable the $ anchor.

C regex extraction

Please consider this C code:
#include <stdio.h>
#include <regex.h>
#include <string.h>
int main(){
char * our_string = "/var/www/html/cameras/cam7/2020-01/15/cam7-2020-01-15-17-45-20-1037-03.h264";
regex_t re;
//int regex_int = regcomp(&re, "cam[:digit:]", 0);
int regex_int = regcomp(&re, "cam", 0);
if (regex_int) {
fprintf(stderr, "regex failed to compile!");
return 1;
}
regmatch_t rm[2];
if ((regexec(&re, our_string, 2, rm,0)) ){
fprintf(stderr, "regex failed to exec!");
return 1;
}
char temp[8192] = {0};
memcpy(temp, our_string + rm[1].rm_so, rm[1].rm_eo - rm[1].rm_so);
printf("We got: %s\n", temp);
puts("Bye!");
return 0;
}
I am trying to extract camX out of our_string, and need help. In its current form, above code is turning blank:
$ ./a.out
We got:
Bye!
C regex is not my forte, Please help!
You have a couple of problems:
//int regex_int = regcomp(&re, "cam[:digit:]", 0)
If you want to match cam followed by a digit, you need (Besides uncommenting this line, of course, and commenting out the one beneath it), to put [:digit:] inside a bracket expression:
int regex_int = regcomp(&re, "cam[[:digit:]]", 0)
The second issue:
memcpy(temp, our_string + rm[1].rm_so, rm[1].rm_eo - rm[1].rm_so);
Neither of your regular expressions have any groups; the second element of the rm array is not going to have anything useful in it. You need to use the first element, which has the offsets of the complete match:
memcpy(temp, our_string + rm[0].rm_so, rm[0].rm_eo - rm[0].rm_so);
You also have a memory leak because you don't have a regfree(&re); to free up memory allocated for the regular expression. Not a big deal in a simple demo program like this, but in something bigger or longer running or that does the matching in a loop, it'll become an issue.

pattern matching / extracting in c using regex.h

I need help extracting a substring from a string using regex.h in C.
In this example, I am trying to extract all occurrences of character 'e' from a string 'telephone'. Unfortunately, I get stuck identifying the offsets of those characters. I am listing code below:
#include <stdio.h>
#include <regex.h>
int main(void) {
const int size=10;
regex_t regex;
regmatch_t matchStruct[size];
char pattern[] = "(e)";
char str[] = "telephone";
int failure = regcomp(&regex, pattern, REG_EXTENDED);
if (failure) {
printf("Cannot compile");
}
int matchFailure = regexec(&regex, pattern, size, matchStruct, 0);
if (!matchFailure) {
printf("\nMatch!!");
} else {
printf("NO Match!!");
}
return 0;
}
So per GNU's manual, I should get all of the occurrences of 'e' when a character is parenthesized. However, I always get only the first occurrence.
Essentially, I want to be able to see something like:
matchStruct[1].rm_so = 1;
matchStruct[1].rm_so = 2;
matchStruct[2].rm_so = 4;
matchStruct[2].rm_so = 5;
matchStruct[3].rm_so = 7;
matchStruct[3].rm_so = 8;
or something along these lines. Any advice?
Please note that you are in fact not comparing your compiled regex against str ("telephone") but rather to your plain-text pattern. Check your second attribute to regexec. That fixed, proceed for instance to "regex in C language using functions regcomp and regexec toggles between first and second match" where the answer to your question is already given.

Resources