上一篇,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个函数原型,可以根据源字符串的表示方式分为三组:
看下参数:
再说说返回值:
给几个简单的示例,演示一下用法。
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,正则表达式就介绍到这里吧。
参考: