The functions used in regular expressions are defined as follows. Use these to perform a search using regular expressions.
#include<regex.h>
int regcomp(regex_t *preg, const char *regex, int cflags);
int regexec(const regex_t *preg, const char *string, size_t nmatch,regmatch_t pmatch[], int eflags);
size_t regerror(int errcode, const regex_t *preg, char *errbuf,size_t errbuf_size);
void regfree(regex_t *preg);
step1 First, use regcomp to compile the regular expression string so that it can be used with regexec. Specify the regular expression string in regex, and the compiled one is stored in preg. cflag is a flag that affects regexec. The meaning is as shown in the table below.
cflag | meaning |
---|---|
REG_EXTENDED | Search with extended regular expressions. |
REG_ICASE | Ignore case. |
REG_NOSUB | Do not tell the matched position. |
REG_NEWLINE | Do not match newlines to operators that match all characters. |
step2 Next, use regexec to search.
For preg, specify the character string that is the search range of the one compiled with regcomp. nmatch and pmatch [] are used to get the matched position. eflags are flags that affect regexec. The meaning is as shown in the table below.
eflag | meaning |
---|---|
REG_NOTBOL | The operator that matches the beginning of the line fails. |
REG_NOTEOL | The operator that matches the end of the line fails. |
regexec returns REG_NOMATCH on failure. It also returns REG_NOMATCH if there is no matching string.
step3
Since regcomp is supposed to save the mallocked area in the regex_t structure, it is finally released with regfree to release it.
regcomp is designed to return an error code on failure. Use regerror to stringify this error code.
Let's make something like a grep command.
example.c
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
int main(int argc, char *argv[]) {
if (argc != 3) {
fprintf(stderr, "usage:\n%s PATTERN FILENAME\n", argv[0]);
exit(1);
}
int err;
char err_str_buf[4096] = {0};
// STEP1
regex_t patbuf;
err = regcomp(&patbuf, argv[1], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
if (err != 0) {
regerror(err, &patbuf, err_str_buf, sizeof(err_str_buf));
fprintf(stderr, "regcomp: %s\n", err_str_buf);
exit(1);
}
FILE *fp;
char buf[1024] = {0};
fp = fopen(argv[2], "r");
if (fp == NULL) {
perror(argv[1]);
exit(1);
}
//STEP2
while(fgets(buf, sizeof(buf), fp)) {
if (regexec(&patbuf, buf, 0, NULL, 0) == 0) {
fputs(buf, stdout);
}
}
//STEP3
regfree(&patbuf);
fclose(fp);
return 0;
}
Execution result
$ gcc -o example_program example.c
$ ./example_program reg.* example.c
#include <regex.h>
regex_t patbuf;
err = regcomp(&patbuf, argv[1], REG_EXTENDED | REG_NOSUB | REG_NEWLINE);
regerror(err, &patbuf, err_str_buf, sizeof(err_str_buf));
fprintf(stderr, "regcomp: %s\n", err_str_buf);
if (regexec(&patbuf, buf, 0, NULL, 0) == 0) {
regfree(&patbuf);
https://linuxjm.osdn.jp/html/LDP_man-pages/man3/regex.3.html
Recommended Posts