前言
定制化开发正则表达式在处理字符定制化开发串方面具有很强大的功能。定制化开发正则表达式是包含一系定制化开发列字符的表达式,定制化开发这些字符定义了可用于字符串、查找或查找/替换算法等的特定搜索模式。正则表达式也用于输入验证。
各个符号的含义
^ $
^:限定开头的字符$:限定结尾的字符
- 1
- 2
\d \D \s \S \w \W
\d:匹配数字字符\D:匹配非数字字符\s:匹配空格\S:匹配非空格\w:匹配一般字符(字母,数字,下划线)\W:匹配除了字母/数字、下划线外的字符
- 1
- 2
- 3
- 4
- 5
- 6
.
.:匹配任意单个字符
- 1
* + ? {…}
*:*前的单个字符可以出现任意次(单个字符包括空格)+:+前的单个字符至少出现一次(单个字符不包括空格)?:?前的单个字符最多出现一次{...}:
- 1
- 2
- 3
- 4
() {} []
()分组:(123),这样可以将匹配到的123取出来{}长度:{4,9},这个表示前一个字符串的长度为4到9[]范围:[a-z],这个表示匹配所有的小写字母
- 1
- 2
- 3
C++中的正则表达式
从C++11开始,C++ 通过标准库提供正则表达式支持。在使用时需要包含头文件regex。
regex_match
regex_match可以理解为全匹配,在写正则时,需要与待匹配字符串格式保持一致。
测试
^ $
1. “^A.*” :以A开头的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^A.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } } else { std::cout << "Not matched" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
结果
2. “.*A$” 以A结尾的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr(".*A$"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "\"; } else { std::cout << "Not matched" << "\" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果
\d \D \s \S \w \W
1. "^\\d." :以数字字符开头的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^\\d.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << ""; } else { std::cout << "Not matched" << "" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果
2. “^\\D.*”:以非数字字符开头的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^\\D.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "\"; } else { std::cout << "Not matched" << "\" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果
3. “.\\s.*” 第二个字符为空格的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr(".\\s.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "\"; } else { std::cout << "Not matched" << "\" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果
4. “.\\S.*” 第二个字符不为空格的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr(".\\S.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "\"; } else { std::cout << "Not matched" << "\" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果
5. “^\\w.*” 以数字/字母/下划线开头的字符串
代码
#include <iostream>#include <regex>#include <string>int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^\\w.*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "\"; } else { std::cout << "Not matched" << "\" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
结果
过滤符合relative-time-in-ms|thread-id|log-level|module-name: [tag]log message规则的日志
代码
int main(int argc, char** argv){ while (1) { std::string inputData; std::getline(std::cin, inputData); std::regex regexStr("^[0-9]*\\|[0-9,a-f]*\\|[V,D,I,W,E,F,T]\\|.*: \\[.*\\].*"); std::smatch matchResult; if (std::regex_match(inputData, matchResult, regexStr)) { std::cout << "Matched" << std::endl; for (auto ele : matchResult) { std::cout << ele << std::endl; } std::cout << "\"; } else { std::cout << "Not matched" << "\" << std::endl; } } return 0;}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
结果
- 55100776|15132|I|nadk-p2p: [ICE]candidate collect
- a5100776|15132|I|nadk-p2p: [ICE]candidate collect
- 55100776|15132|I|nadk-p2p:[ICE]candidate collect
- 55100776||I|nadk-p2p: [ICE]candidate collect