前面介绍了 std::regex来存储正则表达式,
std::regex_match和std::regex_search来使表达式和指定字符串配对.
但是在std::regex_match和std::regex_search匹配到字符串的时候会把匹配的结果存放到std::match_results和std::sub_match.
- std::match_results:
template<class BidirIt,
class Alloc = std::allocator<std::sub_match<BidirIt>>
> class match_results;
由上面的声明可以看出来std::match_results的构造函数不接受任何参数,但是我们必须提供模板参数:
BidirIt的两种提供方式:
1, std::match_results<std::basic_string<char>::const_iterator>;
2, std::match_results<const char*>;
另外我们也可以提供一个allocator!
还有需要我们注意的是我们提供BidirIt的时候注意要么是const_iterator要么就是const char*,为什么会这样呢?翻看了一下介绍发现匹配的结果其实是 指向被匹配的字符串的迭代器 也就是说 如果被匹配的字符串 被销毁了那么 匹配的结果也就不能用了.
std::match_results::match_results
//可以提供一个allocator.
explicit match_results( const Allocator& a = Allocator() );
//拷贝构造函数
match_results( const match_results& rhs );
//移动构造函数.
match_results( match_results&& rhs );
std::match_results::operator=
match_results& operator=( const match_results& other );
match_results& operator=( match_results&& other );
std::match_results::get_allocator
//返回指定的allocator.
allocator_type get_allocator() const;
std::match_results::ready
bool ready() const;
检查std::match_results的状态,默认情况下是not ready的,只有在匹配了regex后才是ready的.
std::match_results::empty
bool empty() const;
检查匹配时候成功,不成功则为empty的返回true.
std::match_results:: size
size_type size() const;
返回匹配到的字符串中子字符串的数目.
#include <iostream>
#include <regex>
int main ()
{
std::string mystring ("subject");
std::smatch mymatches;
std::regex myregex ("(sub)(.*)");
std::regex_match ( mystring, mymatches, myregex );
std::cout << mymatches.size() << " matches found:" << std::endl;
for (unsigned i=0; i<mymatches.size(); ++i)
std::cout << "match #" << i << ": " << mymatches[i] << std::endl;
return 0;
}
3 matches found:
match #0: subject
match #1: sub
match #2: ject
std::match_results::max_size
size_type max_size() const;
返回std::match_results可容纳的最多的子表达式的数量.
std::match_results::length
difference_type length( size_type n = 0 ) const;
返回匹配到的第n个子字符串的长度. 默认为0返回匹配到的字符串的长度.
std::match_results::positon
difference_type position( size_type n = 0 ) const;
将返回 std::distance(prefix().first, (*this)[sub].first);
std::match_results::str
string_type str( size_type n = 0 ) const;
返回匹配到的字符串中的第n个子字符串.
std::match_results::operator[]
const_reference operator[]( size_type n ) const;
返回匹配到的字符串中的第n个子字符串. 也请注意这里的const_reference其实是对std::sub_match.
std::match_results::prefix
const_reference prefix() const;
返回匹配到的字符串的前缀(也就是前面未被匹配到的部分).也请注意这里的const_reference其实是对std::sub_match.
std::match_results::suffix
const_reference suffix() const;
返回匹配到的字符串的后面的未被匹配到的字符串. 也请注意这里的const_reference其实是对std::sub_match.
std::match_results::format
template< class OutputIt >
OutputIter format( OutputIt out,
const char_type* fmt_first, const char_type* fmt_last,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
template< class OutputIt, class ST, class SA >
OutputIter format( OutputIt out,
const basic_string<char_type,ST,SA>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
template< class ST, class SA >
std::basic_string<char_type,ST,SA>
format( const std::basic_string<char_type,ST,SA>& fmt,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
string_type format( const char_type* fmt_s,
std::regex_constants::match_flag_type flags =
std::regex_constants::format_default ) const;
有点类似std::regex_replace.看一个demo:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string s = "for a good time, call 867-5309";
std::regex phone_regex("\\d{3}-\\d{4}");
std::smatch phone_match;
if (std::regex_search(s, phone_match, phone_regex)) {
std::string fmt_s = phone_match.format(
"$`" // $` means characters before the match
"[$&]" // $& means the matched characters
"$'"); // $' means characters following the match
std::cout << fmt_s << '\n';
}
}
- std::sub_match
std::sub_match::sub_match
template<class BidirIt> class sub_match;
也有两种指定模板参数的方式:
1, std::sub_match<std::basic_string<char>::const_iterator>;
2, std::sub_match<const char*>;
std::sub_match::length
difference_type length() const;
返回字符串的长度.
std::sub_match::operator string_type 和 std::sub_match::str()效果一样都是返回一个字符串.