自C++11起标准库提供了正则表达式库,容许咱们使用通配符和pattern来查找和替换掉string中的字符.ios
Match: 将整个string拿来匹配某个regex.web
Search: 查找某个string中与regex吻合的部分.正则表达式
Replace: 将与正则表达式吻合的第一个(或者后续全部的)子序列替换掉.浏览器
Tokenize: 切分即经过指定来切分出来咱们感兴趣的正则表达式匹配到的内容.函数
那么咱们来看一个例子吧,先不用管正则表达式的文法:spa
#include <iostream> #include <regex> #include <string> #include <utility> int main() { std::regex regOne("<.*>.*<.*>"); std::string str("<tg>value</tag>"); std::smatch resultOne; bool booleanOne = std::regex_match(str, resultOne, regOne); std::cout << std::boolalpha << booleanOne << std::endl; std::smatch resultTwo; bool booleanTwo = std::regex_search(str, resultTwo, regOne); std::cout << std::boolalpha << booleanTwo << std::endl; return 0; }
std::regex_match()检查是否整个字符序列匹配某个正则表达式.code
std::regex_search()检查是否部分字符序列匹配某个正则表达式.对象
std::basic_regexip
template <typename charT, typename traits = regex_traits<charT> > class basic_regex;
其中咱们上面使用的std::regex其实就是一个: typedef std::basic_regex<char> regex;ci
此外官方还特例化了一个std::wregex实际上是: typedef std::basic_regex<w_char> wregex;
咱们在构造 std::basic_regex对象的时候除了给出必须给出的正则表达式外海能够指定要给flag:
该flag是个可选的参数,默认值为 ECMAScript(使用的是ECMA-262规范,不少web浏览器用的也是这个标准.
//默认构造函数. basic_regex(); //拷贝构造函数. basic_regex (const basic_regex& rgx); //移动构造函数. basic_regex (basic_regex&& rgx) noexcept; //显式的接受一个C风格字符串的构造函数. explicit basic_regex ( const charT* str, flag_type flags = ECMAScript ); //接受一个C风格的字符串前len个字符做为参数构造一个std::basic_regex对象. basic_regex ( const charT* str, size_t len, flag_type flags = ECMAScript ); //接受一个std::basic_string. template <class ST, class SA> explicit basic_regex ( const basic_string<charT,ST,SA>& str, flag_type flags = ECMAScript ); //接受一对迭代器范围内的字符. template <class ForwardIterator> basic_regex (ForwardIterator first, ForwardIterator last, flag_type flags = ECMAScript ); //接受一个{}括住的字符列表. basic_regex (initializer_list<charT> il, flag_type flags = ECMAScript );
经过上面的构造函数(拷贝构造函数和移动构造函数除外)咱们发现默认的flag都是ECMAScript.
全部的flag都定义在命名空间: std::regex_constants中.
如下均属于: syntax_option_type类型,其实也就是定义在std::regex_constants这个namespace中的enum类型.
std::regex_constants::icase :该flag支持在正则表达式匹配过程当中忽略大小写.
std::regex_constants::nosubs :表flag代表在匹配过程当中不保存匹配的子表达式.
std::regex_constants::optimize :代表正则表达式匹配时候执行速度优于构造速度.
std::regex_constants::ECMAScript : 使用ECMA-262指定的语法.
std::regex_constants::extended : 使用POSIX扩展的正则表达式语法.
std::regex_constants::basic : 使用POSIX的基本正则表达式语法.
std::regex_constants::awk : 使用POSIX版本的awk语言的语法.
std::regex_constants::grep : 使用POSIX版本的grep的语法.
std::regex_constants::egrep : 使用POSIX版本的egrep的语法.
std::basic_regex有几个成员函数让咱们来看看:
std::basic_regex::assign
//等同于拷贝构造函数. basic_regex& assign( const basic_regex& other ); //等同于移动构造函数. basic_regex& assign( basic_regex&& that ); //接受一个C风格的字符串和flag用来构造一个std::basic_regex对象. basic_regex& assign( const CharT* s, flag_type f = std::regex_constants::ECMAScript ); //接受一个c风格的字符串的前count个字符用来构造一个std::basic_regex对象. basic_regex& assign( const charT* ptr, size_t count, flag_type f = std::regex_constants::ECMAScript ); //接受一个std::basic_string和一个flag用来构造. template< class ST, class SA > basic_regex& assign( const std::basic_string<CharT,ST,SA>& str, flag_type f = std::regex_constants::ECMAScript ); //接受一对 iterator范围内的字符用来构造. template< class InputIt > basic_regex& assign( InputIt first, InputIt last, flag_type f = std::regex_constants::ECMAScript ); //接受一个{}括着的字符列表用来构造. template<typename charT> basic_regex& assign( std::initializer_list<CharT> ilist, flag_type f = std::regex_constants::ECMAScript );
std::basic_regex::mark_count
unsigned mark_count() const;
返回正则表达式中子表达式的数目.
std::basic_regex::flags
flag_type flags() const;
返回正则表达式中的flags。
Demo for std::basic_regex
#include <iostream> #include <regex> #include <string> #include <utility> int main() { std::string str("<person>value</person>"); std::basic_regex<char> regex("<(.*)>.*</.*>", std::regex_constants::icase | std::regex_constants::ECMAScript); std::cout << regex.mark_count() << std::endl; std::match_results<std::string::const_iterator> result; bool boolean = std::regex_search(str, result, regex); std::cout << std::boolalpha << "是否匹配成功: " << boolean << " " << regex.mark_count() << std::endl; std::cout << result[0].str() << std::endl; return 0; }