Regex: 正则表达式(3).
2016-08-07 00:00
211 查看
前面介绍了 std::regex来存储正则表达式,
std::regex_match和std::regex_search来使表达式和指定字符串配对.
但是在std::regex_match和std::regex_search匹配到字符串的时候会把匹配的结果存放到std::match_results和std::sub_match.
std::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
std::match_results::operator=
std::match_results::get_allocator
std::match_results::ready
检查std::match_results的状态,默认情况下是not ready的,只有在匹配了regex后才是ready的.
std::match_results::empty
检查匹配时候成功,不成功则为empty的返回true.
std::match_results:: size
返回匹配到的字符串中子字符串的数目.
std::match_results::max_size
返回std::match_results可容纳的最多的子表达式的数量.
std::match_results::length
返回匹配到的第n个子字符串的长度. 默认为0返回匹配到的字符串的长度.
std::match_results::positon
将返回
std::match_results::str
返回匹配到的字符串中的第n个子字符串.
std::match_results::operator[]
返回匹配到的字符串中的第n个子字符串. 也请注意这里的const_reference其实是对std::sub_match.
std::match_results::prefix
返回匹配到的字符串的前缀(也就是前面未被匹配到的部分).也请注意这里的const_reference其实是对std::sub_match.
std::match_results::suffix
返回匹配到的字符串的后面的未被匹配到的字符串. 也请注意这里的const_reference其实是对std::sub_match.
std::match_results::format
有点类似std::regex_replace.看一个demo:
std::sub_match
std::sub_match::sub_match
也有两种指定模板参数的方式:
1, std::sub_match<std::basic_string<char>::const_iterator>;
2, std::sub_match<const char*>;
std::sub_match::length
返回字符串的长度.
std::sub_match::operator string_type 和 std::sub_match::str()效果一样都是返回一个字符串.
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()效果一样都是返回一个字符串.
相关文章推荐
- .NET 正则表达式 Regex
- boost库之正则表达式regex
- Boost学习之正则表达式--regex
- JAVA中正则表达式应用-java.util.regex
- [Language-regex]PHP正则表达式的几则使用技巧
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(2): 关于子表达式
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(5): Compile、Study
- DateFormat Calendar:日历类 正则表达式:(regex)
- iOS中使用RegexKitLite来试用正则表达式
- AE脚本使用正则表达式string.search(regex)
- 个人作品:CuteRegexTool(正则表达式验证工具)
- 邮箱/邮件地址的正则表达式及分析(JavaScript,email,regex)
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(1): 查找
- 身份证号码的正则表达式及验证详解(JavaScript,Regex)
- 在/usr/include/regex.h文件中正则表达式
- .net2.0中的正则表达式的RegexOptions.Compiled选项
- Delphi 正则表达式之TPerlRegEx 类的属性与方法(6): EscapeRegExChars 函数
- 正则表达式(regex)入门、元字符(特殊字符)学习与提高
- Android NDK支持STL的正则表达式 undefined reference to 'std::regex_token_iterator
- 黑马程序员--正则表达式(regex)