我如何在 C 编程中使用正则表达式?例如,如果我想在文件中找到一行
DAEMONS=(sysklogd network sshd !netfs !crond)
Run Code Online (Sandbox Code Playgroud)
然后像这样在单独的行中打印每个守护进程
sysklogd
network
sshd
!netfs
!crond
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止所做的
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#define tofind "[a-z A-Z] $"
int main(){
FILE *fp;
char line[1024];
int retval = 0;
char address[256];
regex_t re;
if(regcomp(&re, tofind, REG_EXTENDED) != 0)
return;
fp = fopen("/etc/rc.conf","r");//this file has this line "DAEMONS=(sysklogd network sshd !netfs !crond)"
while((fgets(line, 1024, fp)) != NULL) {
if((retval = regexec(&re, address, 0, NULL, 0)) == 0)
printf("%s\n", address);
}
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激。
您将这一行读入line,因此您应该将其传递line给regexec()。您还需要考虑行尾的换行符是否会影响模式。(使用 是正确的fgets(),但请记住它在末尾保留换行符。)
您还应该做return -1;(或任何其他不是 0 模 256 的值)而不是return没有值的普通值。此外,您应该检查文件是否已打开;我不得不使用另一个名称,因为我的机器上没有 /etc/rc.conf 这样的文件 - MacOS X。
这对我有用:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <regex.h>
#define tofind "[a-z A-Z] $"
int main(int argc, char **argv)
{
FILE *fp;
char line[1024];
int retval = 0;
regex_t re;
//this file has this line "DAEMONS=(sysklogd network sshd !netfs !crond)"
const char *filename = "/etc/rc.conf";
if (argc > 1)
filename = argv[1];
if (regcomp(&re, tofind, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", tofind);
return EXIT_FAILURE;
}
fp = fopen(filename, "r");
if (fp == 0)
{
fprintf(stderr, "Failed to open file %s (%d: %s)\n",
filename, errno, strerror(errno));
return EXIT_FAILURE;
}
while ((fgets(line, 1024, fp)) != NULL)
{
line[strlen(line)-1] = '\0';
if ((retval = regexec(&re, line, 0, NULL, 0)) == 0)
printf("<<%s>>\n", line);
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
如果您需要帮助编写正则表达式而不是帮助编写使用它们的 C 代码,那么我们需要设计正则表达式以匹配您显示的行。
^DAEMONS=([^)]*) *$
Run Code Online (Sandbox Code Playgroud)
只要按所示方式写入,这将匹配该行。如果你能有“之间的空间S”和“ =”之间或“ =”和“ (”,那么你就需要适当的修改。我允许尾随空格 - 人们通常很草率;但如果他们使用尾随制表符,则不会选择该行。
一旦你找到了这条线,你必须把它分成几部分。您可能会选择使用“捕获”括号工具,或者只是使用strchr()来查找左括号,然后使用合适的技术来分隔守护程序名称 - 我会避免strtok()使用strspn()或strcspn()查找单词。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <regex.h>
#define tofind "^DAEMONS=\\(([^)]*)\\)[ \t]*$"
int main(int argc, char **argv)
{
FILE *fp;
char line[1024];
int retval = 0;
regex_t re;
regmatch_t rm[2];
//this file has this line "DAEMONS=(sysklogd network sshd !netfs !crond)"
const char *filename = "/etc/rc.conf";
if (argc > 1)
filename = argv[1];
if (regcomp(&re, tofind, REG_EXTENDED) != 0)
{
fprintf(stderr, "Failed to compile regex '%s'\n", tofind);
return EXIT_FAILURE;
}
fp = fopen(filename, "r");
if (fp == 0)
{
fprintf(stderr, "Failed to open file %s (%d: %s)\n", filename, errno, strerror(errno));
return EXIT_FAILURE;
}
while ((fgets(line, 1024, fp)) != NULL)
{
line[strlen(line)-1] = '\0';
if ((retval = regexec(&re, line, 2, rm, 0)) == 0)
{
printf("<<%s>>\n", line);
printf("Line: <<%.*s>>\n", (int)(rm[0].rm_eo - rm[0].rm_so), line + rm[0].rm_so);
printf("Text: <<%.*s>>\n", (int)(rm[1].rm_eo - rm[1].rm_so), line + rm[1].rm_so);
char *src = line + rm[1].rm_so;
char *end = line + rm[1].rm_eo;
while (src < end)
{
size_t len = strcspn(src, " ");
if (src + len > end)
len = end - src;
printf("Name: <<%.*s>>\n", (int)len, src);
src += len;
src += strspn(src, " ");
}
}
}
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
那里有大量的调试代码 - 但不会花费你很长时间来产生你要求的答案。我得到:
<<DAEMONS=(sysklogd network sshd !netfs !crond)>>
Line: <<DAEMONS=(sysklogd network sshd !netfs !crond)>>
Text: <<sysklogd network sshd !netfs !crond>>
Name: <<sysklogd>>
Name: <<network>>
Name: <<sshd>>
Name: <<!netfs>>
Name: <<!crond>>
Run Code Online (Sandbox Code Playgroud)
注意:当你想在正则表达式中使用反斜杠时,你必须在 C 源代码中写两个反斜杠。