当前位置:

C++正则表达式

访客 2024-01-05 739 0

前言

正则表达式在处理字符串方面具有很强大的功能。正则表达式是包含一系列字符的表达式,这些字符定义了可用于字符串搜索算法、查找或查找/替换算法等的特定搜索模式。正则表达式也用于输入验证。

各个符号的含义

^$

^:限定开头的字符$:限定结尾的字符

\d\D\s\S\w\W

\d:匹配数字字符\D:匹配非数字字符\s:匹配空格\S:匹配非空格\w:匹配一般字符(字母,数字,下划线)\W:匹配除了字母/数字、下划线外的字符

.

.:匹配任意单个字符

*?{…}

*:*前的单个字符可以出现任意次(单个字符包括空格):前的单个字符至少出现一次(单个字符不包括空格)?:?前的单个字符最多出现一次{...}:

(){}[]

()分组:(123),这样可以将匹配到的123取出来{}长度:{4,9},这个表示前一个字符串的长度为4到9[]范围:[a-z],这个表示匹配所有的小写字母

C中的正则表达式

从C11开始,C通过标准库提供正则表达式支持。在使用时需要包含头文件regex

regex_match

regex_match可以理解为全匹配,在写正则时,需要与待匹配字符串格式保持一致。

测试

^$

1.“^A.*”:以A开头的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr("^A.*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}}else{std::cout<<"Notmatched"<<std::endl;}}return0;}
结果

2.“.*A$”以A结尾的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr(".*A$");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果

\d\D\s\S\w\W

1."^\\d.":以数字字符开头的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr("^\\d.*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果
2.“^\\D.*”:以非数字字符开头的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr("^\\D.*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果
3.“.\\s.*”第二个字符为空格的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr(".\\s.*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果
4.“.\\S.*”第二个字符不为空格的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr(".\\S.*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果

5.“^\\w.*”以数字/字母/下划线开头的字符串
代码
#include<iostream>#include<regex>#include<string>intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr("^\\w.*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果
过滤符合relative-time-in-ms|thread-id|log-level|module-name:[tag]logmessage规则的日志
代码
intmain(intargc,char**argv){while(1){std::stringinputData;std::getline(std::cin,inputData);std::regexregexStr("^[0-9]*\\|[0-9,a-f]*\\|[V,D,I,W,E,F,T]\\|.*:\\[.*\\].*");std::smatchmatchResult;if(std::regex_match(inputData,matchResult,regexStr)){std::cout<<"Matched"<<std::endl;for(autoele:matchResult){std::cout<<ele<<std::endl;}std::cout<<"\n";}else{std::cout<<"Notmatched"<<"\n"<<std::endl;}}return0;}
结果
  • 55100776|15132|I|nadk-p2p:[ICE]candidatecollect
  • a5100776|15132|I|nadk-p2p:[ICE]candidatecollect
  • 55100776|15132|I|nadk-p2p:[ICE]candidatecollect
  • 55100776||I|nadk-p2p:[ICE]candidatecollect

发表评论

  • 评论列表
还没有人评论,快来抢沙发吧~