执行正则表达式搜索并替换std :: string [英] Performing a Regex search and Replace on a std::string

查看:481
本文介绍了执行正则表达式搜索并替换std :: string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模式 XYZ\d\d和一个稍大的字符串,该模式可以多次出现。

I have a pattern '"XYZ\d\d' and a 'largish' string where this pattern can occur many times.

我的目标是在字符串中找到模式的所有实例,然后将匹配的所有字符替换为原始字符串中的字母 A。

My objective is to find all instances of the pattern in the string and then to replace all the characters in that match with the letter 'A' in the original string.

我到目前为止得到以下内容,但是有一个错误:

I've so far got the following however there's an error:

#include <iostream>
#include <regex>

int main() {
    std::regex  exp("XYZ\\d\\d");
    std::smatch res;
    std::string str = " XYZ111 d-dxxxxxxx XYZ222 t-nyyyyyyyyy XYZ333 t-r ";

    auto itr = str.cbegin();

    while (std::regex_search(itr, str.cend(), res, exp)) {

        std::cout << "[" << res[0] << "]" << std::endl;

        for (auto j = res[0].first; j != res[0].second; ++j) {
           *j = 'A';  // Error as dereferencing j causes a const reference
        }

        itr += res.position() + res.length();
    }

    std::cout << std::endl;

    std::cout << "mod: " << str << std::endl;

    return 0;
}

我不确定使用C ++ 11时正确的过程是什么 regex 设施来完成任务。

I'm not sure what the correct process is when using C++11 regex facilities to accomplish my task.

还想知道是否有像 regex_replace 这样的东西需要 functor ,您可以在其中指定每次更改匹配项时如何更改匹配项?

Also was wondering is there something like regex_replace that takes a functor where one can specify how they'd like to change the match on every match occurring?

推荐答案

您需要基于全局正则表达式的替换。这是在没有任何显式循环的情况下执行此操作的三种方法(确保正则表达式替换代码中存在隐式循环):

You need a global regular expression based substitution. Here're three ways to do this without any explicit loops (sure there're "implicit" loops in regex replace codes):

#include <iostream>
#include <string>
#include <regex> // std::regex
#include <pcrecpp.h> // pcrecpp::RE -- needs "-lpcrecpp -lpcre"
#include <pcrscpp.h> // pcrscpp::replace -- needs "-lpcrscpp -lpcre"

int main() {
    std::regex std_rx (R"del(XYZ\d\d)del");
    pcrecpp::RE pcrecpp_rx (R"del(XYZ\d\d)del");
    pcrscpp::replace pcrscpp_rs(R"del(s/XYZ\d\d/A/g)del");
    std::string str = " XYZ111 d-dxxxxxxx XYZ222 t-nyyyyyyyyy XYZ333 t-r ";

    std::cout << "std::regex way: " << std::regex_replace (str, std_rx, "A") << std::endl
              << "pcrecpp way: ";

    std::string buffer(str);
    pcrecpp_rx.GlobalReplace("A", &buffer);

    std::cout << buffer << std::endl
              << "pcrscpp way: ";

    pcrscpp_rs.replace_store(str);
    std::cout << pcrscpp_rs.replace_result << std::endl;

    return 0;
}

结果:

std::regex way:  A1 d-dxxxxxxx A2 t-nyyyyyyyyy A3 t-r
pcrecpp way:  A1 d-dxxxxxxx A2 t-nyyyyyyyyy A3 t-r
pcrscpp way:  A1 d-dxxxxxxx A2 t-nyyyyyyyyy A3 t-r

std :: regex 需要C ++ 11功能,并且在简单模式下的执行速度比PCRE慢大约两倍(请参见此答案),并且我希望在更复杂的库上会更糟,但是只要您使用C ++ 11编译器,就不需要任何其他库。 PCRECPP PCRE C ++包装器。 PCRSCPP 是我对PCRE的封装,它提供了类似Perl的基于正则表达式的替换功能,因此它具有更多优势在此范围内功能比PCRECPP丰富。

std::regex needs C++11 features, and performs about twice slower than PCRE on simple patterns (see this answer), and I expect worse on more complicated ones, but doesn't require any additional libraries, as long as you use a C++11 compiler. PCRECPP is a PCRE C++ wrapper written by Google. PCRSCPP is my wrapper around PCRE that provides Perl-like regular expression based substitution capabilities, and hence is much more feature-rich than PCRECPP in this scope.

这篇关于执行正则表达式搜索并替换std :: string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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