这对你有用:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <regex.h>
int main(void) {
const char tests[2][4] = {"-l4", "-lm"};
const char match[] = "-l[[:digit:]]+";
regex_t rmatch;
if ( regcomp(&rmatch, match, REG_EXTENDED) != 0 ) {
perror("Error compiling regex");
return EXIT_FAILURE;
}
for ( int i = 0; i < 2; ++i ) {
if ( regexec(&rmatch, tests[i], 0, NULL, 0) != 0 ) {
printf("No match for '%s'.\n", tests[i]);
} else {
printf("Matched '%s'.\n", tests[i]);
}
}
return 0;
}
输出:
paul@local:~/src/c/scratch$ ./regex
Matched '-l4'.
No match for '-lm'.
paul@local:~/src/c/scratch$
编辑:在您发布的代码中,您遇到了几个问题:
这一行:
if(regcomp(®ex,"-l[[digit:]]+",0)){
应该:
if( regcomp(®ex, "-l[[:digit:]]+", REG_EXTENDED) ) {
因为您使用的是扩展的正则表达式。如果您更改此行,您的模式将成功匹配。
您的分段错误实际上与您的正则表达式无关,并且来自调用此:
fclose(f);
在您从未成功打开文件的执行路径上时。你应该改变:
FILE *f;
至:
FILE *f = NULL;
并改变:
fclose(f);
至:
if ( f ) {
fclose(f);
}
让自己熟悉 gdb 将有助于您自己跟踪这些事情。
这是您自己的代码的修改版本,它可以工作并包括一些基本的错误检查:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
void reversetext(FILE * f);
int main(int argc, char *argv[]) {
regex_t rmatch;
FILE *f = NULL;
int c;
if ( argc < 2 ) {
printf("You need to enter at least one command line argument.\n");
return EXIT_FAILURE;
}
if ( regcomp(&rmatch, "-l[[:digit:]]+", REG_EXTENDED) ) {
printf("Could not compile regex.\n");
return EXIT_FAILURE;
}
if ( strcmp(argv[1], "-r") == 0 && argc > 2 ) {
printf("argv[1] is -r\n");
if ( (f = fopen(argv[2], "r")) == NULL ) {
fprintf(stderr, "Couldn't open file %s\n", argv[2]);
return EXIT_FAILURE;
}
reversetext(f);
} else if (regexec(&rmatch, argv[1], 0, NULL, 0) == 0) {
printf("Matched '%s' to regex\n", argv[1]);
} else {
if ( (f = fopen(argv[1], "r")) == NULL ) {
fprintf(stderr, "Couldn't open file %s\n", argv[1]);
return EXIT_FAILURE;
}
while ( (c = getc(f)) != EOF) {
printf("%c", c);
}
}
if ( f ) {
fclose(f);
}
}
void reversetext(FILE * f) {
int c = getc(f);
if (c == EOF) {
return;
}
reversetext(f);
printf("%c", c);
}
输出:
paul@local:~/src/c/scratch$ ./regex2 -l4
Matched '-l4' to regex
paul@local:~/src/c/scratch$ ./regex2 -r fakefile
argv[1] is -r
Couldn't open file fakefile
paul@local:~/src/c/scratch$ ./regex2 -tribbles
Couldn't open file -tribbles
paul@local:~/src/c/scratch$ ./regex2 testfile
This is a test.
paul@local:~/src/c/scratch$ ./regex2 -r testfile
argv[1] is -r
.tset a si sihTpaul@local:~/src/c/scratch$
请注意,当您使用getc()
和朋友时,他们使用int
s,而不是char
s。为了能够存储,这是必要的EOF
。
编辑2:根据您评论中的问题,您需要做四件事来匹配一个子组,在这种情况下,匹配的数字部分。
设置一个类型的数组regmatch_t
。您至少需要两个元素,因为第一个元素将匹配整个正则表达式,并且您至少需要一个子组的第二个元素。在下面的代码中,我添加了:
#define MAX_MATCHES 10
regmatch_t m_group[MAX_MATCHES];
在要提取的正则表达式部分周围加上括号。在下面的代码中,我进行了更改:
"-l[[:digit:]]+"
至:
"-l([[:digit:]]+)"
regmatch_t
将您的数组与大小一起传递给regexec()
您调用它时。在下面的代码中,我进行了更改:
} else if (regexec(&rmatch, argv[1], 0, NULL, 0) == 0) {
至:
} else if (regexec(&rmatch, argv[1], MAX_MATCHES, m_group, 0) == 0) {
循环遍历数组并处理每个匹配项。每当数组元素的rm_so
成员regmatch_t
不是-1
时,你就有一个匹配项。我在这里所做的就是将它们复制到缓冲区并打印出来:
} else if ( regexec(&rmatch, argv[1], MAX_MATCHES, m_group, 0) == 0 ) {
printf("Matched '%s' to regex\n", argv[1]);
for ( int i = 0; i < MAX_MATCHES && m_group[i].rm_so != -1; ++i ) {
char buffer[1000] = {0};
char * match_start = &argv[1][m_group[i].rm_so];
size_t match_size = m_group[i].rm_eo - m_group[i].rm_so;
size_t match_len = match_size > 999 ? 999 : match_size;
strncpy(buffer, match_start, match_len);
printf("Matched group %d was '%s'\n", i, buffer);
}
}
这是更新的代码:
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
#define MAX_MATCHES 10
void reversetext(FILE * f);
int main(int argc, char *argv[]) {
regex_t rmatch;
regmatch_t m_group[MAX_MATCHES];
FILE *f = NULL;
int c;
if ( argc < 2 ) {
printf("You need to enter at least one command line argument.\n");
return EXIT_FAILURE;
}
if ( regcomp(&rmatch, "-l([[:digit:]])+", REG_EXTENDED) ) {
printf("Could not compile regex.\n");
return EXIT_FAILURE;
}
if ( strcmp(argv[1], "-r") == 0 && argc > 2) {
printf("argv[1] is -r\n");
if ( (f = fopen(argv[2], "r")) == NULL ) {
fprintf(stderr, "Couldn't open file %s\n", argv[2]);
return EXIT_FAILURE;
}
reversetext(f);
} else if ( regexec(&rmatch, argv[1], MAX_MATCHES, m_group, 0) == 0 ) {
printf("Matched '%s' to regex\n", argv[1]);
for ( int i = 0; i < MAX_MATCHES && m_group[i].rm_so && ; ++i ) {
char buffer[1000] = {0};
char * match_start = &argv[1][m_group[i].rm_so];
size_t match_size = m_group[i].rm_eo - m_group[i].rm_so;
size_t match_len = match_size > 999 ? 999 : match_size;
strncpy(buffer, match_start, match_len);
printf("Matched group %d was '%s'\n", i, buffer);
}
} else {
if ( (f = fopen(argv[1], "r")) == NULL ) {
fprintf(stderr, "Couldn't open file %s\n", argv[1]);
return EXIT_FAILURE;
}
while ( (c = getc(f)) != EOF) {
printf("%c", c);
}
}
if ( f ) {
fclose(f);
}
}
void reversetext(FILE * f) {
int c = getc(f);
if (c == EOF) {
return;
}
reversetext(f);
printf("%c", c);
}
输出:
paul@local:~/src/c/scratch$ ./regex2 -l4
Matched '-l4' to regex
Matched group 0 was '-l4'
Matched group 1 was '4'
paul@local:~/src/c/scratch$