gpt4 book ai didi

c - 使用 PCRS 进行替换的工作示例

转载 作者:行者123 更新时间:2023-11-30 17:49:16 27 4
gpt4 key购买 nike

我需要在 C 中替换一个字符串。这里的答案之一推荐了它 How to do regex string replacements in pure C?使用 PCRS 库。我从这里下载了PCRS ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/Contrib/但我对如何使用它感到困惑。下面是我的代码(取自另一篇 SE 帖子)

            const char *error;
int erroffset;
pcre *re;
int rc;
int i;
int ovector[100];

char *regex = "From:([^@]+).*";
char str[] = "From:regular.expressions@example.com\r\n";
char stringToBeSubstituted[] = "gmail.com";

re = pcre_compile (regex, /* the pattern */
PCRE_MULTILINE,
&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;
}

unsigned int offset = 0;
unsigned int len = strlen(str);
while (offset < len && (rc = pcre_exec(re, 0, str, len, offset, 0, ovector, sizeof(ovector))) >= 0)
{
for(int i = 0; i < rc; ++i)
{
printf("%2d: %.*s\n", i, ovector[2*i+1] - ovector[2*i], str + ovector[2*i]);
}
offset = ovector[1];
}

与“pcre_compile”和“pcre_exec”相反,我需要使用 PCRS 中的哪些函数?

谢谢。

最佳答案

只需按照INSTALL 文件中的说明进行操作即可:

To build PCRS, you will need pcre 3.0 or later and gcc.

Installation is easy: ./configure && make && make install Debug mode can be enabled with --enable-debug.

There is a simple demo application (pcrsed) included.

PCRS 提供了手册页 pcrs.3 中记录的以下函数:

  • pcrs_compile
  • pcrs_compile_command
  • pcrs_execute
  • pcrs_execute_list
  • pcrs_free_job
  • pcrs_free_joblist
  • pcrs_strerror

这是一个online version手册页的。要使用这些函数,请包含头文件 pcrs.h 并使用链接器标志 -lpcrs 将您的程序链接到 PCRS 库。

关于c - 使用 PCRS 进行替换的工作示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17966136/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com