@zhengyuhong
2015-06-08T10:55:21.000000Z
字数 7803
阅读 1595
C++11 STL Boost
由于反斜杠对于C++字符和regex来说都是特殊字符,因而需要转义两次,才能正确执行。而在python中可以直接使用patn = r'/s',r表示禁止转义,所以patn的正则表达式跟作者的意图一致,而C++中没有r这个禁止转义符号,所以需要将反斜杠转义才能正确表达出作者的正则表达式意图。
typedef basic_regex<char> regex;
typedef basic_regex<wchar_t> wregex;
template <class charT, class traits = regex_traits<charT> > class basic_regex;
basic_regex();basic_regex (const basic_regex& rgx);basic_regex (basic_regex&& rgx) noexcept;explicit basic_regex (const charT* str, flag_type flags = ECMAScript );basic_regex (const charT* str, size_t len, flag_type flags = ECMAScript );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 );
// basic_regex constructors// note: using regex, a standard alias of basic_regex<char>#include <iostream>#include <string>#include <regex>int main (){std::string pattern = "^.*$";std::regex first;// defaultstd::regex second = first;// copystd::regex third (pattern);// string object initializationstd::regex fourth ("<[^>]>");// string literal initializationstd::regex fifth (pattern.begin(),pattern.end());// range initializationstd::regex sixth {'.','+'};// initializer_list initializationstd::regex seventh ("[0-9A-Z]+", std::regex::ECMAScript);// with syntax optionusing namespace std::regex_constants;// introducing constants namespacestd::regex eighth ("[0-9A-Z]+", ECMAScript);// same as seventhstd::regex ninth ("\\bd\\w+", ECMAScript | icase );// multiple flagsstd::string subject = "Duddy the duck";std::string replacement = "yup";std::cout << std::regex_replace (subject, ninth, replacement);std::cout << std::endl;return 0;}
一般来说,正则表达式最难的地方就是写出正确的正则表达式,所以regex的构造函数是最为重要的。std::basic_regex还有其他公共成员函数,仅仅列出来,不一一介绍。详述查看reference
template <class charT, class traits>bool regex_match (const charT* s,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class ST, class SA, char charT, class traits>bool regex_match (const basic_string<charT,ST,SA>& s,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class charT, class Alloc, class traits>bool regex_match (const charT* s,match_results<const charT*, Alloc>& m,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
详述查看reference
// regex_match example#include <iostream>#include <string>#include <regex>int main (){if (std::regex_match ("subject", std::regex("(sub)(.*)") ))std::cout << "string literal matched\n";const char cstr[] = "subject";std::string s("subject");std::regex e("(sub)(.*)");if (std::regex_match (s,e))std::cout << "string object matched\n";if ( std::regex_match ( s.begin(), s.end(), e ) )std::cout << "range matched\n";std::cmatch cm;// same as std::match_results<const char*> cm;std::regex_match (cstr,cm,e);std::cout << "string literal with " << cm.size() << " matches\n";// using explicit flags:std::regex_match ( cstr, cm, e, std::regex_constants::match_default );std::smatch sm;// same as std::match_results<string::const_iterator> sm;std::regex_match (s,sm,e);std::cout << "string object with " << sm.size() << " matches\n";std::regex_match ( s.cbegin(), s.cend(), sm, e);std::cout << "range with " << sm.size() << " matches\n";std::cout << "the matches were: ";for (unsigned i=0; i<sm.size(); ++i) {std::cout << "[" << sm[i] << "] ";}std::cout << std::endl;return 0;}
template <class charT, class traits>bool regex_search (const charT* s,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class ST, class SA, char charT, class traits>bool regex_search (const basic_string<charT,ST,SA>& s,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class charT, class Alloc, class traits>bool regex_search (const charT* s,match_results<const charT*, Alloc>& m,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class ST, class SA, class Alloc, class charT, class traits>bool regex_search (const basic_string<charT,ST,SA>& s,match_results<typename basic_string<charT,ST,SA>::const_iterator,Alloc>& m,const basic_regex<charT,traits>& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
example
// regex_search example#include <iostream>#include <string>#include <regex>int main (){std::string s ("this subject has a submarine as a subsequence");std::smatch m;std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"std::cout << "Target sequence: " << s << std::endl;std::cout << "Regular expression: /\\b(sub)([^ ]*)/" << std::endl;std::cout << "The following matches and submatches were found:" << std::endl;while (std::regex_search (s,m,e)) {for (auto x:m) {std::cout << x << " ";}std::cout << std::endl;s = m.suffix().str();}return 0;}
template <class traits, class charT>basic_string<charT> regex_replace (const charT* s,const basic_regex<charT,traits>& rgx,const charT* fmt,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class traits, class charT, class ST, class SA>basic_string<charT> regex_replace (const charT*s,const basic_regex<charT,traits>& rgx,const basic_string<charT,ST,SA>& fmt,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class traits, class charT, class ST, class SA>basic_string<charT,ST,SA> regex_replace (const basic_string<charT,ST,SA>& s,const basic_regex<charT,traits>& rgx,const charT* fmt,regex_constants::match_flag_type flags = regex_constants::match_default);
template <class traits, class charT, class ST, class SA, class FST, class FSA>basic_string<charT,ST,SA> regex_replace (const basic_string<charT,ST,SA>& s,const basic_regex<charT,traits>& rgx,const basic_string<charT,FST,FSA>& fmt,regex_constants::match_flag_type flags = regex_constants::match_default);
example
// regex_replace example#include <iostream>#include <string>#include <regex>#include <iterator>int main (){std::string s ("there is a subsequence in the string\n");std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"// using string/c-string (3) version:std::cout << std::regex_replace (s,e,"sub-$2");// using range/c-string (6) version:std::string result;std::regex_replace (std::back_inserter(result), s.begin(), s.end(), e, "$2");std::cout << result;// with flags:std::cout << std::regex_replace (s,e,"$1 and $2",std::regex_constants::format_no_copy);std::cout << std::endl;return 0;}
$n 后向引用,rgx=("\b(sub)([^ ]*)"),所以理应有两个匹配项,$1表示匹配到第一个字符串,$2表示匹配到第二个字符串。std::regex_replace (s,e,"sub-$2");表示将$1匹配到的字符使用"sub-"替换,$2保持不变
regex_iterator();//The default constructor (1) constructs an end-of-sequence iterator. This value shall not be dereferenced.regex_iterator (const regex_iterator& rit);regex_iterator (BidirectionalIterator first, BidirectionalIterator last,const regex_type& rgx,regex_constants::match_flag_type flags = regex_constants::match_default);
#include <iostream>#include <string>#include <regex>int main (){std::string s ("this subject has a submarine as a subsequence");std::regex e ("\\b(sub)([^ ]*)"); // matches words beginning by "sub"std::regex_iterator<std::string::iterator> rit ( s.begin(), s.end(), e );std::regex_iterator<std::string::iterator> rend;while (rit!=rend) {std::cout << rit->str() << std::endl;++rit;}return 0;}
template <class BidirectionalIterator>class sub_match : public pair <BidirectionalIterator, BidirectionalIterator>;typedef sub_match<const char*> csub_match;//sub_match for string literalstypedef sub_match<const wchar_t*> csub_match;//sub_match for wide string literalstypedef sub_match<string::const_iterator> ssub_match;//sub_match for stringstypedef sub_match<wstring::const_iterator> ssub_match;//sub_match for wide strings
Stores each of the individual matches of a match_results object filled by one of the regex algorithms regex_match or regex_search, or by the regex iterators
#include <iostream>#include <regex>int main (){std::cmatch m; // default constructorstd::regex_match ( "subject", m, std::regex("sub(.*)") );for (std::csub_match sub_m : m)std::cout << "match " << ": " << sub_m << std::endl;return 0;}