仅用一个正则表达式进行多次替换

Multiple replacement with just one regex

让我们假设,为了简单起见,我们有以下字符串:

"John loves Mary, Mary loves Jake and Jake doesn't care about John and Mary."

假设我想使用正则表达式来更改那个故事的角色。

约翰 -> 约瑟夫

玛丽 -> 杰西卡

杰克 -> 基思

当然我可以更改其中的哪一个,一次一个。

但我想知道是否可以只用一个正则表达式替换来更改所有这些,例如 "multiple replacement" 或 "conditional replacement"。

类似于:

正则表达式:(?:(?<name1>John)|(?<name2>Mary)|(?<name3>Jake))

替换:(?(name1)Joseph|(?(name2)Jessica|(?(name3)Keith)))

这只是一个简单的例子。

在我的应用程序中,我必须为每个字符串执行大约 20 次替换,这会影响应用程序的性能。

我使用的正则表达式风格是 PCRE。

该应用程序是使用带有 Qt 框架的 C++ 编写的。

所以您使用的是所谓的 PCRE 风格。很好,除了这并不能准确说明您使用的是哪个库。让我们在这里回顾几个选项,因为一些不同的库声称与 Perl 兼容。

提升

这是最简单的解决方案。 boost::regex 通过 Boost-Extended Format String Syntax.

完全支持您的要求

所以你可以替换模式:

(?<name1>John)|(?<name2>Mary)|(?<name3>Jake)

替换字符串:

(?{name1}Joseph:(?{name2}Jessica:Keith))

当然可以。您可以在 Notepad++ 中对其进行测试,但这里有一些示例代码:

#include <string>
#include <iostream>
#include <boost/regex.hpp>

int main(int argc, char **argv) {
    std::string subject("John loves Mary, Mary loves Jake and Jake doesn't care about John and Mary.");
    const char* replacement = "(?{name1}Joseph:(?{name2}Jessica:Keith))";

    boost::regex re("(?<name1>John)|(?<name2>Mary)|(?<name3>Jake)", boost::match_perl);

    std::string result = boost::regex_replace(subject, re, replacement, boost::format_all);
    std::cout << result << std::endl;

    return 0;
}

PCRE2

PCRE catched up with Boost 并通过 PCRE2_SUBSTITUTE_EXTENDED 引入了更丰富的替换语法。从这个 post (v10.20) 开始,这个代码还没有发布,但是它在源存储库(修订版 381)中可用,所以如果你现在需要这个解决方案,你必须从构建 PCRE2来源。

模式相同,但替换字符串的语法不同:

${name1:+Joseph:${name2:+Jessica:Keith}}

这是一些示例 C 代码:

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

#define PCRE2_CODE_UNIT_WIDTH 8
#include <pcre2.h>

int main(int argc, char **argv) {
    int error;
    PCRE2_SIZE erroffset;

    const PCRE2_SPTR pattern = (PCRE2_SPTR)"(?<name1>John)|(?<name2>Mary)|(?<name3>Jake)";
    const PCRE2_SPTR subject = (PCRE2_SPTR)"John loves Mary, Mary loves Jake and Jake doesn't care about John and Mary.";
    const PCRE2_SPTR replacement = (PCRE2_SPTR)"${name1:+Joseph:${name2:+Jessica:Keith}}";

    pcre2_code *re = pcre2_compile(pattern, PCRE2_ZERO_TERMINATED, 0, &error, &erroffset, 0);
    if (re == 0)
        return 1;

    pcre2_jit_compile(re, PCRE2_JIT_COMPLETE);

    PCRE2_UCHAR output[1024] = "";
    PCRE2_SIZE outlen = sizeof(output) / sizeof(PCRE2_UCHAR);

    int rc = pcre2_substitute(re, subject, PCRE2_ZERO_TERMINATED, 0, PCRE2_SUBSTITUTE_GLOBAL | PCRE2_SUBSTITUTE_EXTENDED, 0, 0, replacement, PCRE2_ZERO_TERMINATED, output, &outlen);
    if (rc >= 0)
        printf("%s\n", output);

    pcre2_code_free(re);
    return 0;
}

PCRE

使用 PCRE (

...这意味着如果这是您正在使用的库,那么无论如何您都可以完全控制替换过程。您可以使用如下模式:

John(*MARK:1)|Mary(*MARK:2)|Jake(*MARK:3)

然后,通过区分最后遇到的 MARK.

Qt

Qt 的QRegularExpressionclass 封装了PCRE 库(不是PCRE2),但似乎并没有公开PCRE 的所有特性。

无论如何,接受 QRegularExpressionQString::replace overload 看起来功能不全:

QString & QString::replace(const QRegularExpression & re, const QString & after)

所以你在这里是一个人。

我的 2 美分

嘿,也许对于这样一个简单的替换,正则表达式是大材小用...如果您遇到性能问题,您应该尝试手动实现这些替换 - 精心设计的算法应该比正则表达式解决方案更快.只需确保分析您的代码并查看罪魁祸首。