C++11开始支持正则表达式了,使得文本更加简单高效。
C++11支持以下六种正则表达式语法
- ECMAScript
most powerful
- basic(POSIX Basic Regular Expressions)
- extended(POSIX Extended Regular Expressions )
- awk(POSIX awk)
- grep(POSIX grep )
- egrep(POSIX grep –E)
C++11 regex的基本模板类
- basic_regex
正则表达式的模板类,包含两个特化类:
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
- match_result `给定正则表达式的匹配序列,match[0]代表整个匹配序列,match[1]代表第1个匹配子序列,match[2]代表第2个匹配子序列,…match[n]代表第n个匹配子序列。n由子序列左括号从左到右的顺序决定,包含以下特化类:
typedef match_results<const char*> cmatch;
typedef match_results<const wchar_t*> wcmatch;
typedef match_results<string::const_iterator> smatch;
typedef match_results<wstring::const_iterator> wsmatch;
- sub_match
该模板类用来表示与一个已标记的子表达式匹配的序列。这个匹配是通过一个迭代器对来表示的,该迭代器对表明了已匹配的正则表达式的一个范围。可以特化为下面几种情况:
typedef sub_match<const char*> csub_match;
typedef sub_match<const wchar_t*> wcsub_match;
typedef sub_match<string::const_iterator> ssub_match;
typedef sub_match<wstring::const_iterator> wssub_match;
C++11 regex的基本模板函数(正则算法)
- regex_match
template<typename _Ch_traits, typename _Ch_alloc,
typename _Alloc, typename _Ch_type,
typename _Rx_traits>
inline bool
regex_match(
const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
match_results<typename basic_string<_Ch_type, _Ch_traits,
_Ch_alloc>::const_iterator, _Alloc>& __m,
const basic_regex<_Ch_type, _Rx_traits>& __re,
regex_constants::match_flag_type __flags
= regex_constants::match_default);
regex_match 匹配整个__s,匹配成功返回true,否则返回false
- regex_search
template<typename _Ch_traits, typename _Ch_alloc,
typename _Alloc, typename _Ch_type,
typename _Rx_traits>
inline bool
regex_search(
const basic_string<_Ch_type, _Ch_traits, _Ch_alloc>& __s,
match_results<typename basic_string<_Ch_type,
_Ch_traits, _Ch_alloc>::const_iterator, _Alloc>& __m,
const basic_regex<_Ch_type, _Rx_traits>& __e,
regex_constants::match_flag_type __f
= regex_constants::match_default);
regex_search类似于regex_match,但它不要求整个字符序列完全匹配,你可以用 regex_search 来查找输入中的一个子序列。
- regex_replace
template<typename _Rx_traits, typename _Ch_type,
typename _St, typename _Sa>
inline basic_string<_Ch_type, _St, _Sa>
regex_replace(
const basic_string<_Ch_type, _St, _Sa>& __s,
const basic_regex<_Ch_type, _Rx_traits>& __e,
const _Ch_type* __fmt,
regex_constants::match_flag_type __flags
= regex_constants::match_default);
regex_replace 在整个字符序列中查找正则表达式__e的所有匹配。这个算法每次成功匹配后,就根据参数fmt对匹配字符串进行格式化。缺省情况下,不匹配的文本不会被修改,即文本会被输出但没有改变。
C++11 regex的迭代器类
迭代器介绍:正则表达式迭代器用来遍历这个正则表达式序列,通过一个迭代器区间来表示匹配的区间。
- regex_iterator
typedef regex_iterator<const char*> cregex_iterator;
typedef regex_iterator<const wchar_t*> wcregex_iterator;
typedef regex_iterator<string::const_iterator> sregex_iterator;
typedef regex_iterator<wstring::const_iterator> wsregex_iterator;
- regex_token_iterator
typedef regex_token_iterator<const char*> cregex_token_iterator;
typedef regex_token_iterator<const wchar_t*> wcregex_token_iterator;
typedef regex_token_iterator<string::const_iterator> sregex_token_iterator;
typedef regex_token_iterator<wstring::const_iterator> wsregex_token_iterator;