码迷,mamicode.com
首页 > 其他好文 > 详细

正则表达式

时间:2017-09-20 14:41:03      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:完全   char   range   正则   res   return   highlight   auto   c++11   

// 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";  
  
  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 ("subject",cm,e);  
  std::cout << "string literal with " << cm.size() << " matches\n";  
  
  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";  
  
  // using explicit flags:  
  std::regex_match ( "subject", cm, e, std::regex_constants::match_default );  
  
  std::cout << "the matches were: ";  
  for (unsigned i=0; i<sm.size(); ++i) {  
    std::cout << "[" << sm[i] << "] ";  
  }  
  
  std::cout << std::endl;  
  
  return 0;  
}  
string literal matched  
string object matched  
range matched  
string literal with 3 matches  
string object with 3 matches  
range with 3 matches  
the matches were: [subject] [sub] [ject] 
// regex_search example  
#include <iostream>  
#include <regex>  
#include <string>  
  
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.begin();x!=m.end();x++)   
      std::cout << x->str() << " ";  
    std::cout << "--> ([^ ]*) match " << m.format("$2") <<std::endl;  
    s = m.suffix().str();  
  }  
} 
Target sequence: this subject has a submarine as a subsequence  
Regular expression: /\b(sub)([^ ]*)/  
The following matches and submatches were found:  
subject sub ject --> ([^ ]*) match ject  
submarine sub marine --> ([^ ]*) match marine  
subsequence sub sequence --> ([^ ]*) match sequence  
#include <regex>   
#include <iostream>   
   
int main() {   
    char buf[20];   
    const char *first = "axayaz";   
    const char *last = first + strlen(first);   
    std::regex rx("a");   
    std::string fmt("A");   
    std::regex_constants::match_flag_type fonly =   
        std::regex_constants::format_first_only;   
   
    *std::regex_replace(&buf[0], first, last, rx, fmt) = \0;   
    std::cout << &buf[0] << std::endl;   
   
    *std::regex_replace(&buf[0], first, last, rx, fmt, fonly) = \0;   
    std::cout << &buf[0] << std::endl;   
   
    std::string str("adaeaf");   
    std::cout << std::regex_replace(str, rx, fmt) << std::endl;   
   
    std::cout << std::regex_replace(str, rx, fmt, fonly) << std::endl;   
   
    return 0;   
}   
AxAyAz  
Axayaz  
AdAeAf  
Adaeaf  

在字符匹配方面:C++11或者是14,有点忘记了,好像是加了R"..."这个中间的字符不需要考虑转义字符,单斜杠也能解析出来

读者可以自己试验一下,我自己也没有试验完全。

正则表达式

标签:完全   char   range   正则   res   return   highlight   auto   c++11   

原文地址:http://www.cnblogs.com/xiaohaige/p/7560500.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!