c - 在 C 中使用 PCRE 在文件中搜索和替换

标签 c regex replace

我想用 C 解析一个 shell 风格的键值配置文件,并根据需要替换值。 示例文件可能如下所示

FOO="test"
SOME_KEY="some value here"
ANOTHER_KEY="here.we.go"
SOMETHING="0"
FOO_BAR_BAZ="2"

要查找值,我想使用正则表达式。我是 PCRE 库的初学者,所以我创建了一些代码来测试。这个应用程序有两个参数:第一个是要搜索的键。第二个是要填入双引号的值。

#include <pcre.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

#define OVECCOUNT 30

int main(int argc, char **argv){
    const char *error;
    int   erroffset;
    pcre *re;
    int   rc;
    int   i;
    int   ovector[OVECCOUNT];

    char regex[64];

    sprintf(regex,"(?<=^%s=\\\").+(?<!\\\")", argv[1]);

    char *str;
    FILE *conf;
    conf = fopen("test.conf", "rw");
    fseek(conf, 0, SEEK_END);
    int confSize = ftell(conf)+1;
    rewind(conf);
    str = malloc(confSize);
    fread(str, 1, confSize, conf);
    fclose(conf);
    str[confSize-1] = '\n';

    re = pcre_compile (
         regex,       /* the pattern */
         PCRE_CASELESS | PCRE_MULTILINE, /* default options */
         &error,               /* for error message */
         &erroffset,           /* for error offset */
         0);                   /* use default character tables */

    if (!re) {
        printf("pcre_compile failed (offset: %d), %s\n", erroffset, error);
        return -1;
    }

    rc = pcre_exec (
        re,                   /* the compiled pattern */
        0,                    /* no extra data - pattern was not studied */
        str,                  /* the string to match */
        confSize,          /* the length of the string */
        0,                    /* start at offset 0 in the subject */
        0,                    /* default options */
        ovector,              /* output vector for substring information */
        OVECCOUNT);           /* number of elements in the output vector */

    if (rc < 0) {
        switch (rc) {
            case PCRE_ERROR_NOMATCH:
                printf("String didn't match");
                break;

            default:
                printf("Error while matching: %d\n", rc);
                break;
        }
        free(re);
        return -1;
    }

    for (i = 0; i < rc; i++) {
        printf("========\nlength of vector: %d\nvector[0..1]: %d %d\nchars at start/end: %c %c\n", ovector[2*i+1] - ovector[2*i], ovector[0], ovector[1], str[ovector[0]], str[ovector[1]]);
        printf("file content length is %d\n========\n", strlen(str));
    }
    int newContentLen = strlen(argv[2])+1;
    char *newContent = calloc(newContentLen,1);
    memcpy(newContent, argv[2], newContentLen);

    char *before = malloc(ovector[0]);
    memcpy(before, str, ovector[0]);

    int afterLen = confSize-ovector[1];
    char *after = malloc(afterLen);
    memcpy(after, str+ovector[1],afterLen);

    int newFileLen = newContentLen+ovector[0]+afterLen;
    char *newFile = calloc(newFileLen,1);

    sprintf(newFile,"%s%s%s", before,newContent, after);

    printf("%s\n", newFile);
    return 0;
}

这段代码在某些情况下是有效的,但如果我想替换 FOOANOTHER_KEY,那就有点可疑了。

$ ./search_replace.out FOO baz
========
length of vector: 5
vector[0..1]: 5 10
chars at start/end: b "
file content length is 94
========
FOO="9@baz"
SOME_KEY="some value here"
ANOTHER_KEY="here.we.go"
SOMETHING="0"
FOO_BAR_BAZ="2"

$ ./search_replace.out ANOTHER_KEY insert
========
length of vector: 10
vector[0..1]: 52 62
chars at start/end: h "
file content length is 94
========
FOO="baaar"
SOME_KEY="some value here"
ANOTHER_KEY=")insert"
SOMETHING="0"
FOO_BAR_BAZ="2"

现在,如果我将输入文件的格式稍微更改为

TEST="new inserted"
FOO="test"
SOME_KEY="some value here"

ANOTHER_KEY="here.we.go"
SOMETHING="0"
FOO_BAR_BAZ="2"

代码运行良好。 我不明白为什么代码在这里表现不同。

最佳答案

替换文本之前的额外字符来自未正确以 null 终止 before 字符串。 (正如 Paul R 指出的那样,您没有对整个缓冲区 str 进行空终止。)因此:

char *before = malloc(ovector[0] + 1);
memcpy(before, str, ovector[0]);
before[ovector[0]] = '\0';

无论如何,分配子字符串和复制内容的业务似乎不必要地复杂并且容易出错。例如,somethingLen 变量是否计算终止空字符?有时他们这样做,有时他们不这样做。我建议选择一种表示形式并始终如一地使用它。 (而且你真的应该在不再使用它们之后释放所有分配的缓冲区,并且可能还清理编译的正则表达式。)

通过在“之前”部分使用 %s 格式说明符的精度字段,您可以只为目标缓冲区分配一个分配:

int cutLen = ovector[1] - ovector[0];
int newFileLen = confSize + strlen(argv[2]) - cutLen;
char *newFile = malloc(newFileLen + 1);

snprintf(newFile, newFileLen + 1, "%.*s%s%s", 
    ovector[0], str, argv[2], str + ovector[1]);

或者,如果您不需要临时缓冲区,您可以只对目标文件使用 fprintf

关于c - 在 C 中使用 PCRE 在文件中搜索和替换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25365917/

相关文章:

python - 在饼图中用逗号更改点 matplotlib

fonts - 如何为波斯语创建自定义字体服务

c - 计算机如何区分空终止符的位和整数 0?

javascript - 如果路径名以 . 开头,并且包含 .正则表达式

C#编程如何通过正则表达式对空格和 ","进行分词?

asp.net - IIS URL 将模块 url 重写为小写

string - JSTL 函数来替换字符串中的引号字符?

c - 在线编译器是否在其服务器上生成 a.out/exe 文件?

c - ISO C90 禁止混合声明和代码 sscanf

c - 空格没有被换行符替换