【问题标题】:Regex custom replace function in CC中的正则表达式自定义替换功能
【发布时间】:2018-06-21 00:56:44
【问题描述】:

我一直在修改此代码https://benchmarksgame.alioth.debian.org/u64q/program.php?test=regexredux&lang=gcc&id=3 以创建 pcre 替换功能。

目前我正在尝试给它一个选项,用第一个替换连续出现的多个并拥有此代码(请参阅中间的评论)

static char * fb_subst_updating(fbuf_t * dst, char * src,
    const char * p,
        const char * r) {
    pcre * re;
    pcre_extra * re_ex;
    const char * re_e;
    char * dp;
    int index_last_coincidence = -1;
    int re_eo, m[3], pos, rlen, clen, coincidence_length;
    if (!(re = pcre_compile(p, 0, & re_e, & re_eo, NULL)))
        exit(1);
    re_ex = pcre_study(re, PCRE_STUDY_JIT_COMPILE, & re_e);
    // The for loop iterates though all the coincidences matches.
    for (dst - > len = 0, rlen = strlen(r), pos = 0; pcre_exec(re, re_ex, src, strlen(src), pos, 0, m, 3) >= 0; pos = m[1]) {
        //m[0] and m[1] are the first and second index position 
        //of the coincidence in the iteration
        clen = m[0] - pos;
        if (r[0] == '$') { 

            coincidence_length = m[1] - m[0];
            dp = fb_need(dst, clen + coincidence_length);
            dst - > len += clen + coincidence_length;
            char coincidence_value[coincidence_length];
            memcpy(coincidence_value, & src[m[0]], coincidence_length);
            memcpy(dp, src + pos, clen);
            if (index_last_coincidence == m[0]) {
                index_last_coincidence = m[1];

                // I'm sure that my problem is here
                // This line could be ignored according to my logic,
                // but the result is the same:
                memcpy(dp + clen, coincidence_value, 0);
            } else {
                index_last_coincidence = m[1];
                memcpy(dp + clen, coincidence_value, coincidence_length);
            }
        } else {
            dp = fb_need(dst, clen + rlen);
            dst - > len += clen + rlen;
            memcpy(dp, src + pos, clen);
            memcpy(dp + clen, r, rlen);
        }
    }
    clen = strlen(src) - pos;
    dp = fb_need(dst, clen);
    dst - > len += clen;
    memcpy(dp, src + pos, clen);
    return dst - > buf;
}

我有这个文本输入:

RT @pacobazan_:我 encanta como se revientan cañones entre ellos。 (@LibreriaSur) Muy eeeeeeee de República Burgués con statichipocresía social, donde todo funciona por autobombo, palmadita al amigo y argolla。 (@LibreriaSur)(@LibreriaSur)(@LibreriaSur)Argolla en el cine, argolla en el periodismo, argolla en la tv。 Grupitos de amigos (@LibreriaSur)jugando a ser Talentosos。 RT @MijaelGLP : Unas chelas para celebrar la inauguración de la (@LibreriaSur)esquina de libros de (@LibreriaSur) en Café Julieta con dos maestros:…

模式是这样的:

((@[A-Za-z0-9_]+))

替换为:$1

我得到 atm 的结果是:

RT @pacobazan_:我 encanta como se revientan cañones entre ellos。 (@LibreriaSur) Muy eeeeeeee de República Burgués con statichipocresía social, donde todo funciona por autobombo, palmadita al amigo y argolla。 (@LibreriaSur)

它应该用(@LibreriaSur)替换(@LibreriaSur)(@LibreriaSur)(@LibreriaSur)

在第一次连续巧合后它不会继续复制,并且还没有弄清楚可能是什么。

根据我的逻辑,如果先前重合的初始位置与实际重合的最终位置相同,则不应复制,并在下一次迭代中继续其余的位置。

如果没有连续的巧合或者我使用正常的替换,它工作正常。

我几乎可以肯定这是一个愚蠢的错误,但我只有几天使用 C 并且无法弄清楚。希望有人可以帮助我。

【问题讨论】:

  • “它在第一次连续的巧合后停止,并且还没有弄清楚可能是什么。” 听起来是学习如何使用调试器单步调试代码的好时机并沿途检查变量的值。另请参阅我的编辑,了解如何正确使用降价并确保换行符实际有效。
  • 我目前正在尝试在单独的环境中运行代码,但我仍在配置它。我在 golang 中使用 CGO 运行此代码并且无法轻松调试它。它不会停止,也许我用错了词,它只是不复制更多。
  • 您是否有意使用 html 标签来格式化您的问题的外观,或者这些是输入和所需输出的一部分?如果您希望标签在您的问题中可见(而不是应用),请在选择应该显示为文字代码引用的问题部分后使用按钮 {}
  • 请注意@underscore_d 的编辑,我认为这与我的问题相同。
  • 如果你提供一个最小的例子,你会得到更多/更好的答案。照原样,您的问题有大量代码转储,您要求我们涉足它。取而代之的是,将其提炼成几行不符合您预期的行。

标签: c string replace substring pcre


【解决方案1】:

好吧,我终于找到了一种让它工作的方法,不得不使用时间变量、strcat、strcpy 并与 '\0' 连接来重构所有函数。结果如下:

static char * replace2(char * text,
const char * pattern,
    const char * replace_value) {
char * result;
result = (char * ) malloc(8192);
pcre * re;
pcre_extra * re_ex;
const char * re_e;
int len_text = strnlen(text, strlen(text)), index_last_coincidencia = -1;
char * dp;
int re_eo, m[3], pos, rlen, clen;
if (!(re = pcre_compile(pattern, 0, & re_e, & re_eo, NULL))) exit(1);
re_ex = pcre_study(re, PCRE_STUDY_JIT_COMPILE, & re_e);
for (rlen = strnlen(replace_value, strlen(replace_value)), pos = 0; pcre_exec(re, re_ex, text, len_text, pos, 0, m, 3) >= 0; pos = m[1]) {

    if (replace_value[0] == '$') {
        if (index_last_coincidencia == m[0]) {
            index_last_coincidencia = m[1];
        } else {
            index_last_coincidencia = m[1];
            char * tmp;
            tmp = (char * ) malloc(m[0] - pos);
            strncpy(tmp, & text[pos], m[0] - pos);
            tmp[m[0] - pos] = '\0';
            strcat(result, tmp);
            free(tmp);
            tmp = (char * ) malloc(m[1] - m[0]);
            strncpy(tmp, & text[m[0]], m[1] - m[0]);
            tmp[m[1] - m[0]] = '\0';
            strcat(result, tmp);
            free(tmp);
        }
    } else {
        char * tmp;
        tmp = (char * ) malloc(m[0] - pos);
        strncpy(tmp, & text[pos], m[0] - pos);
        tmp[m[0] - pos] = '\0';
        strcat(result, tmp);
        free(tmp);
        tmp = (char * ) malloc(rlen);
        strncpy(tmp, replace_value, rlen);
        tmp[rlen] = '\0';
        strcat(result, tmp);
        free(tmp);
    }
}
char * tmp;
int size = (strnlen(text, strlen(text)) - pos);
tmp = (char * ) malloc(size);
strncpy(tmp, & text[pos], size);
tmp[size] = '\0';
strcat(result, tmp);
free(tmp);
return result;}

【讨论】:

    猜你喜欢
    • 2020-03-21
    • 1970-01-01
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 2011-03-21
    相关资源
    最近更新 更多