網站首頁 編程語言 正文
在C++11中引入了正則表達式。
字符規則
先來了解一下這個字符的含義吧。
字符 | 描述 |
---|---|
\ | 轉義字符 |
$ | 匹配字符行尾 |
* | 匹配前面的子表達式任意多次 |
+ | 匹配前面的子表達式一次或多次 |
? | 匹配前面的子表達式零次或一次 |
{m} | 匹配確定的m次 |
{m,} | 匹配至少m次 |
{m,n} | 最少匹配m次,最大匹配n次 |
字符 | 描述 |
---|---|
. | 匹配任意字符 |
x|y | 匹配x或y |
[xyz] | 字符集合,匹配包含的任意一個字符 |
[^xyz] | 匹配未包含的任意字符 |
[a-z] | 字符范圍,匹配指定范圍內的任意字符 |
[^a-z] | 匹配任何不在指定范圍內的任意字符 |
頭文件:#include
regex_match
全文匹配,即要求整個字符串符合匹配規則,返回true或false
匹配“四個數字-一個或倆個數字”
#include <iostream>
#include <regex>
using namespace std;
int main()
{
string str;
cin >> str;
//\d 表示匹配數字 {4} 長度4個 \d{1,2}表示匹配數字長度為1-2
cout << regex_match(str, regex("\\d{4}-\\d{1,2}"));
return 0;
}
匹配郵箱 “大小寫字母或數字@126/163.com”
int main()
{
string str;
cout << "請輸入郵箱:" << endl;
while (cin >> str)//匹配郵箱
{
if (true == regex_match(str, regex("[a-zA-Z0-9]+@1(26|63)\\.com")))
{
break;
}
cout << "輸入錯誤,請重新輸入:" << endl;
}
cout << "輸入成功!" << endl;
return 0;
}
regex_search
搜索匹配,即搜索字符串中存在符合規則的子字符串。
用法一:匹配單個
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main()
{
string str = "hello2019-02-03word";
smatch match;//搜索結果
regex pattern("(\\d{4})-(\\d{1,2})-(\\d{1,2})");//搜索規則 ()表示把內容拿出來
if (regex_search(str, match, pattern))
{ //提取 年 月 日
cout << "年:" << match[1] << endl;
cout << "月:" << match[2] << endl;
cout << "日:" << match[3] << endl;
//下標從1開始 下標0存的是符合這個搜索規則的起始位置和結束位置
}
return 0;
}
用法二:匹配多個
#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main()
{
//匹配多個符合要求的字符串
string str = "2019-08-07,2019-08-08,2019-08-09";
smatch match;
regex pattern("(\\d{4})-(\\d{1,2})-(\\d{1,2})");
string::const_iterator citer = str.cbegin();
while (regex_search(citer, str.cend(), match, pattern))//循環匹配
{
citer = match[0].second;
for (size_t i = 1; i < match.size(); ++i)
{
cout << match[i] << " ";
}
cout << endl;
}
return 0;
}
regex_replace
替換匹配,即可以將符合匹配規則的子字符串替換為其他字符串。
將字符串中的-替換為/
#include <iostream>
#include <regex>
using namespace std;
int main()
{
//替換不會修改原串
cout << regex_replace("2019-08-07", regex("-"), "/") << endl;
return 0;
}
匹配以逗號分隔的字符串(\S表示匹配任意顯示字符)
使用正則表達式將所有信息批處理為sql的語句
使用正則表達式1999-10-7 修改為 10/7/1999
先匹配上,再拿小括號獲取值 然后替換
然后就可以放在程序中
int main()
{
string str;
cin >> str;
cout << regex_replace(str, regex("(\\d{4})-(\\d{1,2})-(\\d{1,2})"), "$2/$3/$1");
return 0;
}
將字符串中的/刪掉
總結?
原文鏈接:https://blog.csdn.net/qq_45254369/article/details/125491031
相關推薦
- 2022-03-27 Android自定義模擬時鐘控件_Android
- 2023-02-04 golang?Gorm框架講解_Golang
- 2022-05-24 C#多線程TPL模式下使用HttpClient_C#教程
- 2022-01-12 對比原生Node封裝的Express路由 和 express框架路由
- 2024-01-16 關于jquery中的ajax 網絡異常失敗回調函數
- 2023-12-22 獲取微信小程序版本號,uni
- 2024-03-10 SpringMVC中Model和ModelAndView的區別
- 2022-11-08 PostgreSQL?pg_filenode.map文件介紹_PostgreSQL
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支