C++正则表达式:替换、标志、异常及语法详解
立即解锁
发布时间: 2025-08-22 00:43:52 阅读量: 2 订阅数: 16 


深入解析C++标准库:从入门到精通
### C++正则表达式:替换、标志、异常及语法详解
#### 1. 正则表达式替换
正则表达式替换功能允许我们将匹配正则表达式的字符序列进行替换。以下是一个示例代码:
```cpp
// regex/regexreplace1.cpp
#include <string>
#include <regex>
#include <iostream>
#include <iterator>
using namespace std;
int main()
{
string data = "<person>\n"
" <first>Nico</first>\n"
" <last>Josuttis</last>\n"
"</person>\n";
regex reg("<(.*)>(.*)</(\\1)>");
// print data with replacement for matched patterns
cout << regex_replace (data,
// data
reg,
// regular expression
"<$1 value=\"$2\"/>")
// replacement
<< endl;
// same using sed syntax
cout << regex_replace (data,
// data
reg,
// regular expression
"<\\1 value=\"\\2\"/>",
// replacement
regex_constants::format_sed)
// format flag
<< endl;
// use iterator interface, and
// - format_no_copy:
// don’t copy characters that don’t match
// - format_first_only: replace only the first match found
string res2;
regex_replace (back_inserter(res2),
// destination
data.begin(), data.end(),
// source range
reg,
// regular expression
"<$1 value=\"$2\"/>",
// replacement
regex_constants::format_no_copy
// format flags
| regex_constants::format_first_only);
cout << res2 << endl;
}
```
在这个例子中,我们使用正则表达式匹配 XML/HTML 标签值,并将输入转换为特定的输出格式。我们可以使用 `$` 字符来引用匹配的子表达式,例如 `$1` 和 `$2` 分别代表标签和标签内的值。同时,我们还可以使用 `regex_constants::format_sed` 来使用 UNIX 命令 `sed` 的替换语法。
以下是正则表达式替换符号的含义:
| Default Pattern | sed Pattern | Meaning |
| --- | --- | --- |
| $& | & | 匹配的模式 |
| $n | \n | 第 n 个匹配的捕获组 |
| $‘ | | 匹配模式的前缀 |
| $’ | | 匹配模式的后缀 |
| $$ | | 字符 $ |
#### 2. 正则表达式标志
正则表达式库提供了一些常量,可用于影响正则表达式接口的行为。以下是一些常用的标志及其含义:
| regex_constants | Meaning |
| --- | --- |
| ECMAScript | 使用 ECMAScript 语法(默认) |
| basic | 使用 POSIX 的基本正则表达式(BRE)语法 |
| extended | 使用 POSIX 的扩展正则表达式(ERE)语法 |
| awk | 使用 UNIX 工具 awk 的语法 |
| grep | 使用 UNIX 工具 grep 的语法 |
| egrep | 使用 UNIX 工具 egrep 的语法 |
| icase | 忽略大小写敏感 |
| nosubs | 不在匹配结果中存储子序列 |
| optimize | 优化匹配速度而非正则表达式创建速度 |
| collate | 形如 [a - b] 的字符范围应具有区域敏感性 |
| match_not_null | 空序列不应匹配 |
| match_not_bol | 第一个字符不应匹配行首(模式 ^) |
| match_not_eol | 最后一个字符不应匹配行尾(模式 $) |
| match_not_bow | 第一个字符不应匹配单词首(模式 \b) |
| match_not_eow | 最后一个字符不应匹配单词尾(模式 \b) |
| match_continuous | 表达式应仅匹配以第一个字符开头的子序列 |
| match_any | 如果有多个匹配可能,任何匹配都可接受 |
| match_prev_avail | 第一个字符之前的位置是有效位置(忽略 match_not_bol 和 match_not_bow) |
| format_default | 使用默认(ECMAScript)替换语法 |
| format_sed | 使用 UNIX 工具 sed 的替换语法 |
| format_first_only | 仅替换第一个匹配项 |
| format_no_copy | 不复制不匹配的字符 |
以下是一个使用标志的示例代码:
```cpp
// regex/regex4.cpp
#include <string>
#include <regex>
#include <iostream>
using namespace std;
int main()
{
// case-insensitive find LaTeX index entries
string pat1 = R"(\\.*index\{([^}]*)\})";
// first capture group
string pat2 = R"(\\.*index\{(.*)\}\{(.*)\})";
// 2nd and 3rd capture group
regex pat (pat1+"\n"+pat2,
regex_constants::egrep|regex_constants::icase);
// initialize string with characters from standard input:
string data((istreambuf_iterator<char>(cin)),
istreambuf_iterator<char>());
// search and print matching index entries:
smatch m;
auto pos = data.cbegin();
auto end = data.cend();
for ( ; regex_search (pos,end,m,pat); pos=m.suffix().first) {
cout << "match: " << m.str() << endl;
cout << " val: " << m.str(1)+m.str(2) << endl;
cout << " see: " <<
```
0
0
复制全文
相关推荐









