正则表达式

Posted _xiaohaige

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了正则表达式相关的知识,希望对你有一定的参考价值。

// 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"..."这个中间的字符不需要考虑转义字符,单斜杠也能解析出来

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

以上是关于正则表达式的主要内容,如果未能解决你的问题,请参考以下文章

markdown 正则表达式模式片段

正则表达式匹配特定的 URL 片段而不是所有其他 URL 可能性

循环通过 python 正则表达式匹配

asp.net 使用正则表达式验证包含打开/关闭括号片段的属性字符串

攻破难啃的骨头-正则表达式(转)

正则表达式的贪婪和非贪婪模式