怎么办正则表达式替换字符串纯C? [英] How to do regex string replacements in pure C?

查看:91
本文介绍了怎么办正则表达式替换字符串纯C?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过的POSIX正则表达式库和PCRE库正则表达式的功能,但他们两人似乎并不有一个字符串替换功能。我不想使用C ++,这将是最好的,如果我不需要另一个链接库(但我可以的,如果我有)。我是否需要手动执行字符串替换?如果是这样,我该如何使用捕捉组?

I've looked at the regex functions in the POSIX regex library and the PCRE library, but both of them don't seem to have a string replacement function. I don't want to use C++, and it would be best if I don't need to link another library (but I can if I have to). Do I need to manually do the string replacing? If so, how can I use capture groups?

推荐答案

regex.h不提供字符串替换原生支持,但它确实提供了SUBEX pressions /捕获组,这使得它容易得多。我假设你熟悉正则表达式编译,然后跳到正则表达式执行和SUBEX pressions。

regex.h does not provide native support for string replacement, however it does provide subexpressions/capture groups which make it much easier. I'll assume that you're familiar with regex compilations and skip to regex execution and subexpressions.

regexec()被定义为在regex.h如下(在/ usr /包含/):

regexec() is defined as follows in regex.h (/usr/include/):

extern int regexec (const regex_t *__restrict __preg,
        const char *__restrict __string, size_t __nmatch,
        regmatch_t __pmatch[__restrict_arr],
        int __eflags);

第一,第二和最后的参数是正则表达式,要在与执行的标志分别执行的字符串。第三和第四个参数用于指定regmatch_t的阵列。一个regmatch_t由两个字段组成:rm_so和rm_eo,它们分别是指数,或偏移,匹配区域的开始和结束的。 // EN:论文索引然后可以一起的memcpy(),的 memset的() memmove与()从string.h中执行字符串替换。

The first, second, and final arguments are the regex, string to be executed on and execution flags, respectively. The third and fourth arguments are used to specify an array of regmatch_t's. A regmatch_t consists of two fields: rm_so and rm_eo, which are the indices, or offsets, of the beginning and end of the matched area, respectively. Theses indices can then be used along with memcpy(), memset() and memmove()from string.h to perform string replacement.

我将做一个小例子,后来张贴。

I'll make a little example and post it later.

祝你好运,我希望这有助于。

Good luck, and I hope that this helped.

这篇关于怎么办正则表达式替换字符串纯C?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆