【C++】正则表达式Regex使用

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

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

#include <regex>

1 基本语法

  1. 位置匹配
    ^:表示行的开头。
    $:表示行的结尾。
    \b:表示单词边界。
    \B:表示非单词边界。

  2. 重复次数
    *:表示前面的元素可以出现零次或多次。
    +:表示前面的元素必须至少出现一次。
    ?:表示前面的元素可选,可以出现零次或一次。
    {n}:表示前面的元素必须出现恰好 n 次。
    {n,}:表示前面的元素至少出现 n 次。
    {n,m}:表示前面的元素出现次数在 n 和 m 之间(包括 n 和 m)。

  3. 字符匹配
    [a-zA-Z0-9]: 匹配任意字母或数字。
    . :匹配任意单个字符。如果要匹配普通字符.,需要使用\.
    \: 用于转义特殊字符。

  4. 字符类
    [...]:表示匹配方括号中的任意一个字符。
    [^...]:表示不匹配方括号中的任何一个字符。
    [a-z]:表示匹配从小写 a 到小写 z 之间的任意一个字符。

  5. 特殊转移字符
    \d:表示匹配任意数字。
    \w:表示匹配任意字母、数字或下划线。
    \s:表示匹配任意空白字符。
    \D:表示匹配任意非数字字符。
    \W:表示匹配任意非字母、非数字和非下划线字符。
    \S:表示匹配任意非空白字符。

基本用法

1 匹配正确的MAC地址

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

#include <iostream>
#include <regex>

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 <iostream>
#include <regex>
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 <iostream>
#include <regex>
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 <bits/stdc++.h>
# include <regex>
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;
}
  • 4
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值