正则表达式应用
正则表达式是一种 文本模式匹配工具,用来描述字符串的结构。你可以用它来查找、验证、提取、替换字符串中符合特定格式的内容。
例如,你可以用一个正则表达式验证:
- 邮箱是否合法
- 一个字符串是否是电话号码
- 一个文件路径是否是
.cpp
文件
1 正则表达式基本字符分类
分类 | 示例 | 含义说明 |
---|---|---|
普通字符 | a , b |
匹配自己本身 |
元字符 | . ^ \$ \* + ? | () [] {} |
有特殊含义的控制符 |
转义字符 | \. \* |
匹配特殊字符本身(如 . ) |
2 常用正则元字符语法
语法 | 含义说明 | 示例 |
---|---|---|
. |
匹配任意一个字符(不包括换行) | a.c 可匹配 abc , a@c |
^ |
匹配字符串的开始位置 | ^abc 匹配以 abc 开头的字符串 |
$ |
匹配字符串的结束位置 | xyz$ 匹配以 xyz 结尾的 |
* |
匹配前一个字符出现 0 次或多次 | bo* 匹配 b , bo , boo |
+ |
匹配前一个字符出现 1 次或多次 | go+gle 可匹配 google |
? |
匹配前一个字符出现 0 次或 1 次 | colou?r 匹配 color 或 colour |
{n} |
恰好出现 n 次 | a{3} 匹配 aaa |
{n,} |
至少出现 n 次 | a{2,} 匹配 aa , aaa , … |
{n,m} |
出现 n 到 m 次 | a{2,4} 匹配 aa , aaa , aaaa |
[...] |
匹配集合中的任一字符 | [abc] 匹配 a 或 b 或 c |
[^...] |
不匹配集合中的任一字符 | [^abc] 匹配非 a/b/c |
` | `foo | `bar | 匹配foo 或bar |
( ) |
分组 | (ab)+ 匹配 ab , abab ,… |
3 常用简写字符类(预定义)
简写 | 含义说明 | 示例 |
---|---|---|
\d |
匹配任意数字 [0-9] |
\d{3} 匹配三位数 |
\D |
匹配非数字 | \D+ 匹配字母等 |
\w |
匹配单词字符 [a-zA-Z0-9_] |
匹配变量名等 |
\W |
匹配非单词字符 | 空格、标点等 |
\s |
匹配空白字符(空格、制表符) | \s+ 可匹配多个空格 |
\S |
匹配非空白字符 | 非空内容 |
4 常用匹配实例
目标 | 正则表达式 | 说明 |
---|---|---|
邮箱地址 | [\w.-]+@[\w.-]+\.\w+ |
简单邮箱格式 |
IPv4地址 | \b\d{1,3}(\.\d{1,3}){3}\b |
不严格校验 |
中文字符 | [\u4e00-\u9fa5] (或用UTF-8编码匹配) |
需处理编码 |
手机号(中国) | 1[3-9]\d{9} |
11位以1开头 |
日期(YYYY-MM-DD) | \d{4}-\d{2}-\d{2} |
|
C++ 文件路径 | .*\.cpp$ |
匹配以 .cpp 结尾的字符串 |
URL | https?://[\w./?=&%-]+ |
5 C++ 使用
std::regex pattern(R"(\d{3}-\d{3,4}-\d{4})"); // 匹配电话号码
std::smatch result;
std::string text = "Call me at 010-1234-5678";
if (std::regex_search(text, result, pattern)) {
std::cout << "Match: " << result[0] << "\n";
}
- C++11 标准库:
<regex>
#include <iostream>
#include <regex>
#include <string>
int main() {
std::string text = "My email is test@example.com.";
std::regex pattern(R"(\b[\w.-]+@[\w.-]+\.\w+\b)");
std::smatch match;
if (std::regex_search(text, match, pattern)) {
std::cout << "Found: " << match[0] << std::endl;
} else {
std::cout << "No match found." << std::endl;
}
return 0;
}
说明:
std::regex
:表示一个正则表达式模式。std::smatch
:字符串匹配结果(基于std::string
)。std::regex_search
:搜索匹配位置。std::regex_match
:完全匹配整个字符串。
⚠ 注意:GCC 4.9+ 才支持
<regex>
较完整,老版本不建议使用。
- POSIX 正则库(兼容 C/C++,头文件
<regex.h>
)
#include <iostream>
#include <regex.h>
int main() {
const char* pattern = "^[a-z]+@[a-z]+\\.[a-z]+$";
const char* text = "example@email.com";
regex_t regex;
regcomp(®ex, pattern, REG_EXTENDED | REG_NOSUB);
if (regexec(®ex, text, 0, nullptr, 0) == 0) {
std::cout << "Matched!" << std::endl;
} else {
std::cout << "No match." << std::endl;
}
regfree(®ex);
return 0;
}
特点:
- 兼容所有 Linux 平台,依赖
libc
。 - 不支持 C++ 风格语义。
- 错误信息需要手动处理。
- 使用第三方库(推荐 Boost.Regex)
#include <iostream>
#include <boost/regex.hpp>
int main() {
std::string s = "Your code is at: /home/user/main.cpp";
boost::regex expr("/home/\\w+/\\w+\\.cpp");
if (boost::regex_search(s, expr)) {
std::cout << "Path matched!" << std::endl;
} else {
std::cout << "No match." << std::endl;
}
return 0;
}
编译时需加
-lboost_regex
链接:
g++ test.cpp -o test -lboost_regex
编译参数与环境:
编译 C++11 <regex>
示例:
g++ -std=c++11 regex_std.cpp -o regex_std
POSIX regex:
g++ regex_posix.cpp -o regex_posix
Boost regex:
g++ -std=c++11 regex_boost.cpp -o regex_boost -lboost_regex
6 调试建议
你可以在线调试你的正则表达式:
- https://regex101.com/(支持多语言,包括 C++)
- https://regexr.com/
- VSCode 插件:Regex Previewer
7 C++ 项目中正则应用场景
- 配置文件格式检查
- 日志分析与过滤(grep替代)
- 文件名/路径筛选器
- 命令行参数/表达式提取
- 编译器/解释器中的词法分析器(Lexer)
三种方式应用场景:
场景 | 推荐方式 |
---|---|
简单跨平台程序 | std::regex |
面向 Linux 嵌入式/C 项目 | POSIX regex.h |
高性能/复杂正则处理 | Boost.Regex |