C++11 | 正则表达式(3)

本文详细介绍了C++11中的regex_replace函数,包括其六个不同的函数原型,以及如何使用它们进行字符串替换。通过实例展示了如何将源字符串中的特定模式替换成新的字符串,涉及迭代器、std::string和C风格字符串的不同用法。此外,还提及了OutputIterator的概念,并给出了std::ostream_iterator和自定义StrOutputIterator的使用示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

上一篇,C++11 | 正则表达式(2)介绍了regex_search的用法,这次看看regex_replace吧。

regex_replace方法原型如下:

//(1)   
template< class OutputIt, class BidirIt,
          class Traits, class CharT,
          class STraits, class SAlloc >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
                        const std::basic_regex<CharT,Traits>& re,
                        const std::basic_string<CharT,STraits,SAlloc>& fmt,
                        std::regex_constants::match_flag_type flags = 
                            std::regex_constants::match_default );

// (2)  
template< class OutputIt, class BidirIt,
          class Traits, class CharT >
OutputIt regex_replace( OutputIt out, BidirIt first, BidirIt last,
                        const std::basic_regex<CharT,Traits>& re,
                        const CharT* fmt,
                        std::regex_constants::match_flag_type flags = 
                            std::regex_constants::match_default );

// (3)  
template< class Traits, class CharT,
          class STraits, class SAlloc,
          class FTraits, class FAlloc >
std::basic_string<CharT,STraits,SAlloc> 
    regex_replace( const std::basic_string<CharT,STraits,SAlloc>& s,
                   const std::basic_regex<CharT,Traits>& re,
                   const std::basic_string<CharT,FTraits,FAlloc>& fmt,
                   std::regex_constants::match_flag_type flags = 
                       std::regex_constants::match_default );

// (4)  
template< class Traits, class CharT,
          class STraits, class SAlloc >
std::basic_string<CharT,STraits,SAlloc> 
    regex_replace( const std::basic_string<CharT,STraits,SAlloc>& s,
                   const std::basic_regex<CharT,Traits>& re,
                   const CharT* fmt,
                   std::regex_constants::match_flag_type flags = 
                       std::regex_constants::match_default );

// (5)
template< class Traits, class CharT,
          class STraits, class SAlloc >
std::basic_string<CharT> 
    regex_replace( const CharT* s,
                   const std::basic_regex<CharT,Traits>& re,
                   const std::basic_string<CharT,STraits,SAlloc>& fmt,
                   std::regex_constants::match_flag_type flags = 
                       std::regex_constants::match_default );

// (6)
template< class Traits, class CharT >
std::basic_string<CharT> 
    regex_replace( const CharT* s,
                   const std::basic_regex<CharT,Traits>& re,
                   const CharT* fmt,
                   std::regex_constants::match_flag_type flags = 
                       std::regex_constants::match_default );

6个函数原型,可以根据源字符串的表示方式分为三组:

  • 1,2两个使用迭代器作为输入
  • 3,4使用string作为输入
  • 5,6使用null terminated字符串(C风格字符串)作为输入

看下参数:

  • first, last,一对迭代器,界定了要替换的字符串范围
  • s,源(作为输入的)字符串,std::basic_string的各种变体或char*的各种变体
  • re,std::basic_regex的各种变体
  • fmt,一个正则表达式字符串,用于替换匹配到的源串。这个字符串的格式取决于后面的flags。最常见的就是字面值字符串替换。
  • flags, std::regex_constants::match_flag_type定义的标记
  • out,储存替换结果的迭代器

再说说返回值:

  • 1,2这两个原型,返回out迭代器的一个拷贝
  • 3~6返回替换后的字符串

给几个简单的示例,演示一下用法。

  • 先看第一个
    std::string strEx = "I like QT , it\'s a great framework written in C++! qT is perfect!";
    std::regex reQt("QT|qT|qt");

    std::string result = std::regex_replace(strEx, reQt, "Qt", std::regex_constants::match_default);
    std::cout << "result - " << result << "\n";

上面的代码片段,把示例字符串strEx中的QT、qT等替换为Qt,使用了第4个函数原型。

  • 再看第二代码片段
class StrOutputIterator : public std::iterator<std::output_iterator_tag, char>
{
public:
    StrOutputIterator(char *p) : m_p(p)
    {}
    StrOutputIterator(const StrOutputIterator &rhs) : m_p(rhs.m_p)
    {}

    // support it++, +1, return modified this
    StrOutputIterator& operator ++()
    {
        ++m_p;
        return *this;
    }
    // support ++it, return orig, then +1
    StrOutputIterator operator ++(int)
    {
        StrOutputIterator tmp(*this);
        operator++();
        return tmp;
    }

    StrOutputIterator& operator +=(int n){ m_p += n; return *this; }
    StrOutputIterator operator +(int n) { return StrOutputIterator( m_p + n);}

    bool operator == (const StrOutputIterator &rhs) { return m_p = rhs.m_p; }
    bool operator != (const StrOutputIterator &rhs) { return m_p != rhs.m_p; }
    char &operator *(){ return *m_p; }

private:
    char * m_p;
};

    std::regex reQt2("[qQ][tT]");
    char *szOut = new char[strEx.size() + 1];
    szOut[strEx.size()] = 0;
    StrOutputIterator iter(szOut);
    std::cout << "result2 - \n";
    std::regex_replace(iter, strEx.begin(), strEx.end(), reQt2, "Qt");
    std::cout << szOut << '\n';
    delete [] szOut;

这次我使用的是第2个原型,它需要一个OutputIterator。所谓OutputIterator,就是一个可以写入的迭代器。std::ostream_iterator实现了OutputIterator,待会提供一个简单示例,直接把正则替换的结果写到标准输出上。

我自己实现了一个StrOutputIterator(代码就在上面),可以把替换后的结果放到一个char*中。

  • 现在看第三个片段
    std::cout << "result3 - ";
    std::regex_replace(std::ostreambuf_iterator<char>(std::cout), strEx.begin(), strEx.end(), reQt2, "Qt");

这个代码片段使用了ostream_iterator,比我们自己实现的StrOutputIterator简单多了。

Ok,正则表达式就介绍到这里吧。


参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

foruok

你可以选择打赏

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值