开发语言 【C++】正则表达式Regex使用 - #include

📅 2026-11-30 👤 拼多多助力营销技巧 📂 2027拼多多助力 👁️ 404 次阅读

正则表达式(Regular Expression),简称为Regex,用于匹配、查找或替换文本中符合特定模式的字符串。

C++中使用Regex需要包含相关头文件

#include

1 基本语法

位置匹配 ^:表示行的开头。 $:表示行的结尾。 \b:表示单词边界。 \B:表示非单词边界。 重复次数 *:表示前面的元素可以出现零次或多次。 +:表示前面的元素必须至少出现一次。 ?:表示前面的元素可选,可以出现零次或一次。 {n}:表示前面的元素必须出现恰好 n 次。 {n,}:表示前面的元素至少出现 n 次。 {n,m}:表示前面的元素出现次数在 n 和 m 之间(包括 n 和 m)。 字符匹配 [a-zA-Z0-9]: 匹配任意字母或数字。 . :匹配任意单个字符。如果要匹配普通字符.,需要使用\.。 \: 用于转义特殊字符。 字符类 [...]:表示匹配方括号中的任意一个字符。 [^...]:表示不匹配方括号中的任何一个字符。 [a-z]:表示匹配从小写 a 到小写 z 之间的任意一个字符。 特殊转移字符 \d:表示匹配任意数字。 \w:表示匹配任意字母、数字或下划线。 \s:表示匹配任意空白字符。 \D:表示匹配任意非数字字符。 \W:表示匹配任意非字母、非数字和非下划线字符。 \S:表示匹配任意非空白字符。

基本用法

1 匹配正确的MAC地址

MAC地址以十六进制表示,每个字节使用两个十六进制数字表示,例如 00:11:22:AA:BB:CC

#include

#include

using namespace std;

bool isValidMacAddress(const string& mac) { 拼多多帮助力入口

regex pattern("^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$");

return regex_match(mac, pattern);

}

int main() {

string mac1 = "00:11:22:AA:BB:CC";

string mac2 = "00-11-22-AA-BB-CC";

string mac3 = "00-11-22-AA-BB-CCC";

string mac4 = "001122AABBCC";

if (isValidMacAddress(mac1)) {

cout << mac1 << " True" << endl;

}

else {

cout << mac1 << " False" << endl;

}

if (isValidMacAddress(mac2)) {

cout << mac2 << " True" << endl;

}

else {

cout << mac2 << " False" << endl;

}

if (isValidMacAddress(mac3)) {

cout << mac3 << " True" << endl;

}

else {

cout << mac3 << " False" << endl;

}

if (isValidMacAddress(mac4)) {

cout << mac4 << " True" << endl;

}

else {

cout << mac4 << " False" << endl;

}

return 0;

}

2 匹配IPv4地址

正则表达式pattern中可以分为四个部分

25[0-5]:以25开头,并且后面跟着0到5之间的数字,即250-255。2[0-4]\\d:以2开头,后面跟着0到4之间的数字,然后是任意一位数字(0-9),即200-249。1\\d\\d:以1开头,后面跟着任意两位数字(0-9),即100-199。[1-9]\\d:以非零数字开头,后面跟着任意一位数字(0-9),即10-99。\\d:匹配单个数字(0-9)。 注意:\d表示匹配任何字符,\\d表示对\d的转义。

#include

#include

using namespace std;

bool isValidIPv4( string& s) {

s += ".";

regex pattern("((25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]\\d|\\d)\.){4}");

return regex_match(s, pattern);

}

int main() {

string s1 = "255.255.255.289";

string s2 = "1.2.3.4";

if (isValidIPv4(s1)) {

cout << "IPv4正确" << endl;

}

else {

cout << "IPv4错误" << endl;

}

if (isValidIPv4(s2)) {

cout << "IPv4正确" << endl;

}

else {

cout << "IPv4错误" << endl;

}

return 0;

}

3 验证邮箱格式

其中,在正则表达式匹配中使用的R"( )" 是C++11引入的原始字符串字面量表示法,允许在字符串中使用特殊字符而无需进行额外的转移。

#include

#include

using namespace std;

bool isValidEmailAddress(const string& email) {

regex pattern(R"(\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}\b)");

return regex_match(email, pattern);

}

int main() {

string email1 = "example@example.com";

string email2 = "example@example.com.cn";

if (isValidEmailAddress(email1)) {

cout << "邮箱格式正确" << endl;

}

else {

cout << "邮箱格式错误" << endl;

}

if (isValidEmailAddress(email2)) {

cout << "邮箱格式正确" << endl;

}

else {

cout << "邮箱格式错误" << endl;

}

return 0;

}

4 提取字符串

用来匹配以‘+’‘-'或者数字直接起头的数字字符串

#include

# include

using namespace std;

int main() {

string input = " gfggrg//+322456";

regex pattern(R"([+-]?[0-9]+)");

// 在输入字符串中搜索匹配的子串

smatch matches;

if (regex_search(input, matches, pattern)) {

// 提取第一个匹配的子串

string s = matches[0];

cout << s << endl;

}

return 0;

}

参考文章

标签: 拼多多助力网站平台 赚钱项目 拼多多助力网 首码项目 手赚APP
拼多多助力营销技巧