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

C++11 | 正则表达式(1)介绍了C++11中的正则表达式类库,举了一个迭代器的示例,演示使用regex、sregex_iterator、smatch的用法。这次来看看regex_search的用法。

regex_search方法的原型如下:

//(1) 
template< class BidirIt, 
          class Alloc, class CharT, class Traits >
bool regex_search( BidirIt first, BidirIt last,
    std::match_results<BidirIt,Alloc>& m,
    const std::basic_regex<CharT,Traits>& e,
    std::regex_constants::match_flag_type flags = 
    std::regex_constants::match_default );

//(2)
template< class CharT, class Alloc, class Traits >
bool regex_search( const CharT* str,
    std::match_results<const CharT*,Alloc>& m,
    const std::basic_regex<CharT,Traits>& e,
    std::regex_constants::match_flag_type flags = 
    std::regex_constants::match_default );
//(3)
template< class STraits, class SAlloc,
          class Alloc, class CharT, class Traits >
bool regex_search( const std::basic_string<CharT,STraits,SAlloc>& s,
    std::match_results<
        typename std::basic_string<CharT,STraits,SAlloc>::const_iterator, 
        Alloc>& m,
    const std::basic_regex<CharT, Traits>& e,
    std::regex_constants::match_flag_type flags =
        std::regex_constants::match_default );

//(4)
template< class BidirIt, class CharT, class Traits >
bool regex_search( BidirIt first, BidirIt last,
    const std::basic_regex<CharT,Traits>& e,
    std::regex_constants::match_flag_type flags = 
        std::regex_constants::match_default );

//(5) 
template< class CharT, class Traits >
bool regex_search( const CharT* str,
    const std::basic_regex<CharT,Traits>& e,
    std::regex_constants::match_flag_type flags = 
    std::regex_constants::match_default );

//(6) 
template< class STraits, class SAlloc,
          class CharT, class Traits >
bool regex_search( const std::basic_string<CharT,STraits,SAlloc>& s,
    const std::basic_regex<CharT,Traits>& e,
    std::regex_constants::match_flag_type flags = 
    std::regex_constants::match_default );

C++14里还有一个原型,参看http://en.cppreference.com/w/cpp/regex/regex_search。

前三个函数原型是类似的,先说后三个参数:

  • e,std::regex,要应用在目标字符串上的正则表达式
  • m,匹配的结果
  • flags,定义正则搜索行为的标记,类型为 std::regex_constants::match_flag_type。

再来倒着说其它参数:

  • first, last,双向迭代器,标注待匹配的字符序列范围
  • str,null-terminated格式的字符串
  • s, std::string

后三个函数原型,与前三个差不多,就是少了个m,少了m的结果就是,你只能得到有没有匹配上这个状态,匹配到的结果没办法获取。

我们给个简单的示例,演示下1、3、6这三个regex_search的用法,代码如下:

    std::string strEx = "Long long ago, there was a temple on the hill, there was a monk live in the temple.";

    // case 1.
    std::regex literal_regex("temple",
        std::regex_constants::ECMAScript |
        std::regex_constants::icase);
    // prototype (6)
    if(std::regex_search(strEx, literal_regex))
    {
        std::cout << "text contains the phrase \'temple\'\n";
    }

    // case 2.
    // use std::smatch && loop to find all words whose length == 3.
    // std::smatch --> 
    // std::match_results<std::string::const_iterator>
    std::cout << "Words longer equals 3 :\n";
    std::regex word_len3_regex("\\b\\w{3}\\b");
    std::smatch m;
    std::string::const_iterator it = strEx.begin();
    std::string::const_iterator end = strEx.end();

    // prototype (1)
    while(std::regex_search(it, end, m, word_len3_regex))
    {
        if(m.ready() && m.size())
        {
            std::cout << m[0].str() << std::endl;
            it = m[0].second;
        }
    }

    // case 3.
    // use prototype (3), change string to iterate
    std::string ex = strEx;
    while(std::regex_search(ex, m, word_len3_regex))
    {
        if(m.ready() && m.size())
        {
            std::cout << m[0].str() << std::endl;
            ex = m.suffix().str();
        }
    }
    std::cout << "strEx = " << ex << std::endl;    

Ok,这次到这里,下次介绍下regex_replace。

参考

  • C++11 | 正则表达式(1)
  • C++11 | range-based for loop
  • C++11 | 自动类型推断——auto
  • C++11 | 运行时类型识别(RTTI)

你可能感兴趣的:(正则表达式,regex,C++11)