|
| 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 | +} |
0 commit comments