Skip to content

Commit a5bd556

Browse files
committed
tweaks
1 parent ed6bc6e commit a5bd556

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
cmake_minimum_required(VERSION 3.16.3)
3+
project(regex)
4+
add_executable(regex1
5+
regex1.c)
6+
add_executable(regex2
7+
regex2.c)
8+
9+

dateRegex.c

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* dateRegex.c
3+
*
4+
* Copyright 2023 Michael <michael@michael-Inspiron-1501>
5+
*
6+
* This program is free software; you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation; either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program; if not, write to the Free Software
18+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19+
* MA 02110-1301, USA.
20+
*
21+
*
22+
*/
23+
// 2023/01/22 19:50:27
24+
// https://regex-generator.olafneumann.org/?sampleText=2020-03-12T13%3A34%3A56.123Z%20INFO%20%20%5Borg.example.Class%5D%3A%20This%20is%20a%20%23simple%20%23logline%20containing%20a%20%27value%27.&flags=i
25+
#include <stdio.h>
26+
27+
int main(int argc, char **argv)
28+
{
29+
int res;
30+
res = useRegex("2020-03-12T13:34:56\\.123Z INFO \\[org\\.example\\.Class]: This is a #simple #logline containing a 'value'\\.");
31+
return 0;
32+
}
33+
34+
#include <regex.h>
35+
36+
int useRegex(char* textToCheck) {
37+
regex_t compiledRegex;
38+
int reti;
39+
int actualReturnValue = -1;
40+
char messageBuffer[100];
41+
42+
/* Compile regular expression */
43+
reti = regcomp(&compiledRegex, "2020-03-12T13:34:56\\.123Z INFO \\[org\\.example\\.Class]: This is a #simple #logline containing a 'value'\\.", REG_EXTENDED | REG_ICASE);
44+
if (reti) {
45+
fprintf(stderr, "Could not compile regex\n");
46+
return -2;
47+
}
48+
49+
/* Execute compiled regular expression */
50+
reti = regexec(&compiledRegex, textToCheck, 0, NULL, 0);
51+
if (!reti) {
52+
puts("Match");
53+
actualReturnValue = 0;
54+
} else if (reti == REG_NOMATCH) {
55+
puts("No match");
56+
actualReturnValue = 1;
57+
} else {
58+
regerror(reti, &compiledRegex, messageBuffer, sizeof(messageBuffer));
59+
fprintf(stderr, "Regex match failed: %s\n", messageBuffer);
60+
actualReturnValue = -3;
61+
}
62+
63+
/* Free memory allocated to the pattern buffer by regcomp() */
64+
regfree(&compiledRegex);
65+
return actualReturnValue;
66+
}

regex5.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <errno.h>
5+
#include <regex.h>
6+
7+
#define tofind "^daemons=\\(([^)]*)\\)[ \t]*$"
8+
9+
int main(int argc, char **argv)
10+
{
11+
FILE *fp;
12+
char line[1024];
13+
int retval = 0;
14+
regex_t re;
15+
regmatch_t rm[2];
16+
//this file has this line "DAEMONS=(sysklogd network sshd !netfs !crond)"
17+
const char *filename = "/etc/rc1.d"; // was /etc/rc.conf
18+
19+
if (argc > 1)
20+
filename = argv[1];
21+
22+
if (regcomp(&re, tofind, REG_EXTENDED) != 0)
23+
{
24+
fprintf(stderr, "Failed to compile regex '%s'\n", tofind);
25+
return EXIT_FAILURE;
26+
}
27+
printf("Regex: %s\n", tofind);
28+
printf("Number of captured expressions: %zu\n", re.re_nsub);
29+
30+
fp = fopen(filename, "r");
31+
if (fp == 0)
32+
{
33+
fprintf(stderr, "Failed to open file %s (%d: %s)\n", filename, errno, strerror(errno));
34+
return EXIT_FAILURE;
35+
}
36+
37+
while ((fgets(line, 1024, fp)) != NULL)
38+
{
39+
line[strcspn(line, "\n")] = '\0';
40+
if ((retval = regexec(&re, line, 2, rm, 0)) == 0)
41+
{
42+
printf("<<%s>>\n", line);
43+
// Complete match
44+
printf("Line: <<%.*s>>\n", (int)(rm[0].rm_eo - rm[0].rm_so), line + rm[0].rm_so);
45+
// Match captured in (...) - the \( and \) match literal parenthesis
46+
printf("Text: <<%.*s>>\n", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
47+
char *src = line + rm[1].rm_so;
48+
char *end = line + rm[1].rm_eo;
49+
while (src < end)
50+
{
51+
size_t len = strcspn(src, " ");
52+
if (src + len > end)
53+
len = end - src;
54+
printf("Name: <<%.*s>>\n", (int)len, src);
55+
src += len;
56+
src += strspn(src, " ");
57+
}
58+
}
59+
}
60+
return EXIT_SUCCESS;
61+
}

url1.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
https://regexr.com/
2+
3+
Sun 22 Jan 2023 19:55:12
4+
https://www.javainuse.com/rexgenerator
5+
6+

xed1.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
banana

0 commit comments

Comments
 (0)