C 11是什么意思?能否详细解释一下?

2026-04-10 08:451阅读0评论SEO问题
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计1255个文字,预计阅读时间需要6分钟。

C 11是什么意思?能否详细解释一下?

参考文章1:C++11 — 快速学会正则表达式参考文章2:C++与正则表达式参考文章3:C++正则表达式regex库使用方法总结

头文件:cpp#include

参考文章①: zhuanlan.zhihu.com/p/181695725

参考文章②: C++11 – 快速学会正则表达式

参考文章③: C++与正则表达式

C 11是什么意思?能否详细解释一下?

参考文章④: C++正则表达式regex库使用方法总结

头文件

头文件: #include \ 也可以写万能头文件: #include \<bits/stdc++.h>

正则表达式规则

{n} 匹配确定的n次 {n,} 至少匹配n次(注:请不要擅自加空格上去) {n,m} 最少n次,最多m次. * 匹配前面的子表达式0次或多次 = {0,} + 匹配前面的子表达式1次或多次 = {1,} ? 匹配前面的子表达式0次或1次 = {0,1} () 表示一个整体 [] 表示一位 {} 表示匹配多少次 . 匹配除换行符\n之外的任意字符 \w 匹配单字字符 (a-z,A-Z,0-9以及下划线) \W 匹配非单字字符 \s 匹配空白字符 (空格,制表符,换行符) \S 匹配非空白字符 \d 匹配数字字符 \D 匹配非数字字符 ^ 指示从行的开始位置开始匹配 $ 指示从行的结束位置开始匹配


匹配(Match)

字符串处理常用的一个操作是匹配,即字符串和规则恰好对应,而用于匹配的函数 regex_match,它是个函数模板, 注意: 找到返回 ture, 找不到返回 false:

bool regex_match(string s, regex pattern) bool regex_match(string s, smatch result, regex pattern) bool regex_match(s.cbegin() + i, s.cend(), smatch result, regex pattern) // 从字符串的某个位置开始匹配

直接看例子:

#include <iostream> #include <regex> using namespace std; int main() { regex pattern("^1[3578]\\d{9}$"); // 1开头,然后数字[3578]中的一个,后面还有九个数字 string str = "17779637570"; smatch result; bool ismatch = regex_match(str, result, pattern); if (ismatch) { cout << "匹配成功:" << result[0] << endl; // 输出: 匹配成功:17779637570 } else { cout << "匹配失败" << endl; } }

搜索(Search)

搜索其对应的函数为 regex_search,也是个函数模板,注意: 当找到第一个符合条件的子串后, 就立即返回结果

bool regex_search(string s, regex pattern) bool regex_search(string s, smatch result, regex pattern) bool regex_search(s.cbegin() + i, s.cend(), smatch result, regex pattern) //从字符串的某个位置开始匹配

搜索给定字符串中是否存在与模式匹配的子串,如果存在则返回 true

#include <iostream> #include <regex> using namespace std; int main() { regex pattern("\\d+"); // 匹配一个到无穷个数字 string str= "581x41+(5-13/2)x3a"; smatch result; string::const_iterator iter_begin = str.cbegin(); string::const_iterator iter_end = str.cend(); while (regex_search(iter_begin, iter_end, result, pattern)) { // 举例: () cout << "查找成功:" << result[0] << endl; // 581; 41; 5; ... cout << "查找结果子串的在源串中的迭代器位置: " << *result[0].first << endl; // 5 ; 4; 5; ... 相当于查找到的字符串的cbegin cout << "查找结果子串的在源串后面的位置: " << *result[0].second << endl; // x ; +; -; ... 相当于查找到的字符串的cend❗ iter_begin = result[0].second; //更新迭代器位置 } }

问题: 提取所有的日期: xxxx:xx:xx 并输出年、月、日

#include <iostream> #include <regex> using namespace std; int main() { string str = " 2022:4:25 星期一\n 2022:4:26 星期二\n 2022:4:27 星期三"; regex pattern("([0-9]{4}):([0-9]{1,2}):([0-9]{1,2})"); // 匹配日期 (假设日期格式正确) // 用小括号来分组, 表示分别匹配年月日 // 方法一 // smatch result; // string::const_iterator iter_begin = str.cbegin(); // string::const_iterator iter_end = str.cend(); // while (regex_search(iter_begin, iter_end, result, pattern)) { // cout << result[0] << " --- "; // cout << result[1] << "年" << result[2] << "月" << result[3] << "日" << endl; // iter_begin = result[0].second; // 更新搜索起始位置,搜索剩下的字符串 // } // 方法二: 利用sregex_iterator迭代器, 它将反复调用regex_search()来寻找文件中的所有匹配 for (sregex_iterator it(str.begin(), str.end(), pattern), end_it; it != end_it; it++) { cout << it->str() << " --- "; cout << it->str(1) << "年" << it->str(2) << "月" << it->str(3) << "日" << endl; } } /* 结果: 2022:4:25 --- 2022年4月25日 2022:4:26 --- 2022年4月26日 2022:4:27 --- 2022年4月27日 */

替换(Replace)

最后一种操作称为替换,即将正则表达式内容替换为指定内容,regex库用模板函数 regex_replace 提供替换操作。

string regex_replace(string s, regex pattern, string replace_str)

现在,给定一个数据为"he...ll..o, worl..d!", 思考一下,如何去掉其中误敲的“."

#include <iostream> #include <regex> using namespace std; int main() { string str = "he...ll..o, worl..d!"; regex pattern("\\."); // 匹配点. cout << regex_replace(str, pattern, ""); // 将匹配到的点替换成无,即删除点 // 输出: hello, world! }

本文共计1255个文字,预计阅读时间需要6分钟。

C 11是什么意思?能否详细解释一下?

参考文章1:C++11 — 快速学会正则表达式参考文章2:C++与正则表达式参考文章3:C++正则表达式regex库使用方法总结

头文件:cpp#include

参考文章①: zhuanlan.zhihu.com/p/181695725

参考文章②: C++11 – 快速学会正则表达式

参考文章③: C++与正则表达式

C 11是什么意思?能否详细解释一下?

参考文章④: C++正则表达式regex库使用方法总结

头文件

头文件: #include \ 也可以写万能头文件: #include \<bits/stdc++.h>

正则表达式规则

{n} 匹配确定的n次 {n,} 至少匹配n次(注:请不要擅自加空格上去) {n,m} 最少n次,最多m次. * 匹配前面的子表达式0次或多次 = {0,} + 匹配前面的子表达式1次或多次 = {1,} ? 匹配前面的子表达式0次或1次 = {0,1} () 表示一个整体 [] 表示一位 {} 表示匹配多少次 . 匹配除换行符\n之外的任意字符 \w 匹配单字字符 (a-z,A-Z,0-9以及下划线) \W 匹配非单字字符 \s 匹配空白字符 (空格,制表符,换行符) \S 匹配非空白字符 \d 匹配数字字符 \D 匹配非数字字符 ^ 指示从行的开始位置开始匹配 $ 指示从行的结束位置开始匹配


匹配(Match)

字符串处理常用的一个操作是匹配,即字符串和规则恰好对应,而用于匹配的函数 regex_match,它是个函数模板, 注意: 找到返回 ture, 找不到返回 false:

bool regex_match(string s, regex pattern) bool regex_match(string s, smatch result, regex pattern) bool regex_match(s.cbegin() + i, s.cend(), smatch result, regex pattern) // 从字符串的某个位置开始匹配

直接看例子:

#include <iostream> #include <regex> using namespace std; int main() { regex pattern("^1[3578]\\d{9}$"); // 1开头,然后数字[3578]中的一个,后面还有九个数字 string str = "17779637570"; smatch result; bool ismatch = regex_match(str, result, pattern); if (ismatch) { cout << "匹配成功:" << result[0] << endl; // 输出: 匹配成功:17779637570 } else { cout << "匹配失败" << endl; } }

搜索(Search)

搜索其对应的函数为 regex_search,也是个函数模板,注意: 当找到第一个符合条件的子串后, 就立即返回结果

bool regex_search(string s, regex pattern) bool regex_search(string s, smatch result, regex pattern) bool regex_search(s.cbegin() + i, s.cend(), smatch result, regex pattern) //从字符串的某个位置开始匹配

搜索给定字符串中是否存在与模式匹配的子串,如果存在则返回 true

#include <iostream> #include <regex> using namespace std; int main() { regex pattern("\\d+"); // 匹配一个到无穷个数字 string str= "581x41+(5-13/2)x3a"; smatch result; string::const_iterator iter_begin = str.cbegin(); string::const_iterator iter_end = str.cend(); while (regex_search(iter_begin, iter_end, result, pattern)) { // 举例: () cout << "查找成功:" << result[0] << endl; // 581; 41; 5; ... cout << "查找结果子串的在源串中的迭代器位置: " << *result[0].first << endl; // 5 ; 4; 5; ... 相当于查找到的字符串的cbegin cout << "查找结果子串的在源串后面的位置: " << *result[0].second << endl; // x ; +; -; ... 相当于查找到的字符串的cend❗ iter_begin = result[0].second; //更新迭代器位置 } }

问题: 提取所有的日期: xxxx:xx:xx 并输出年、月、日

#include <iostream> #include <regex> using namespace std; int main() { string str = " 2022:4:25 星期一\n 2022:4:26 星期二\n 2022:4:27 星期三"; regex pattern("([0-9]{4}):([0-9]{1,2}):([0-9]{1,2})"); // 匹配日期 (假设日期格式正确) // 用小括号来分组, 表示分别匹配年月日 // 方法一 // smatch result; // string::const_iterator iter_begin = str.cbegin(); // string::const_iterator iter_end = str.cend(); // while (regex_search(iter_begin, iter_end, result, pattern)) { // cout << result[0] << " --- "; // cout << result[1] << "年" << result[2] << "月" << result[3] << "日" << endl; // iter_begin = result[0].second; // 更新搜索起始位置,搜索剩下的字符串 // } // 方法二: 利用sregex_iterator迭代器, 它将反复调用regex_search()来寻找文件中的所有匹配 for (sregex_iterator it(str.begin(), str.end(), pattern), end_it; it != end_it; it++) { cout << it->str() << " --- "; cout << it->str(1) << "年" << it->str(2) << "月" << it->str(3) << "日" << endl; } } /* 结果: 2022:4:25 --- 2022年4月25日 2022:4:26 --- 2022年4月26日 2022:4:27 --- 2022年4月27日 */

替换(Replace)

最后一种操作称为替换,即将正则表达式内容替换为指定内容,regex库用模板函数 regex_replace 提供替换操作。

string regex_replace(string s, regex pattern, string replace_str)

现在,给定一个数据为"he...ll..o, worl..d!", 思考一下,如何去掉其中误敲的“."

#include <iostream> #include <regex> using namespace std; int main() { string str = "he...ll..o, worl..d!"; regex pattern("\\."); // 匹配点. cout << regex_replace(str, pattern, ""); // 将匹配到的点替换成无,即删除点 // 输出: hello, world! }