您的位置:首页 > 编程语言 > C语言/C++

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

2016-07-21 13:18 423 查看
上一篇,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,正则表达式就介绍到这里吧。

参考:

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

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

C++11 | range-based for loop

C++11 | 自动类型推断——auto

C++11 | 运行时类型识别(RTTI)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: