C++11正則表達(dá)式詳解(regex_match、regex_search和regex_replace)
在C++11中引入了正則表達(dá)式。
字符規(guī)則
先來了解一下這個字符的含義吧。
字符 | 描述 |
---|---|
\ | 轉(zhuǎn)義字符 |
$ | 匹配字符行尾 |
* | 匹配前面的子表達(dá)式任意多次 |
+ | 匹配前面的子表達(dá)式一次或多次 |
? | 匹配前面的子表達(dá)式零次或一次 |
{m} | 匹配確定的m次 |
{m,} | 匹配至少m次 |
{m,n} | 最少匹配m次,最大匹配n次 |
字符 | 描述 |
---|---|
. | 匹配任意字符 |
x|y | 匹配x或y |
[xyz] | 字符集合,匹配包含的任意一個字符 |
[^xyz] | 匹配未包含的任意字符 |
[a-z] | 字符范圍,匹配指定范圍內(nèi)的任意字符 |
[^a-z] | 匹配任何不在指定范圍內(nèi)的任意字符 |
頭文件:#include
regex_match
全文匹配,即要求整個字符串符合匹配規(guī)則,返回true或false
匹配“四個數(shù)字-一個或倆個數(shù)字”
#include <iostream> #include <regex> using namespace std; int main() { string str; cin >> str; //\d 表示匹配數(shù)字 {4} 長度4個 \d{1,2}表示匹配數(shù)字長度為1-2 cout << regex_match(str, regex("\\d{4}-\\d{1,2}")); return 0; }
匹配郵箱 “大小寫字母或數(shù)字@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
搜索匹配,即搜索字符串中存在符合規(guī)則的子字符串。
用法一:匹配單個
#include <iostream> #include <regex> #include <string> using namespace std; int main() { string str = "hello2019-02-03word"; smatch match;//搜索結(jié)果 regex pattern("(\\d{4})-(\\d{1,2})-(\\d{1,2})");//搜索規(guī)則 ()表示把內(nèi)容拿出來 if (regex_search(str, match, pattern)) { //提取 年 月 日 cout << "年:" << match[1] << endl; cout << "月:" << match[2] << endl; cout << "日:" << match[3] << endl; //下標(biāo)從1開始 下標(biāo)0存的是符合這個搜索規(guī)則的起始位置和結(jié)束位置 } 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))//循環(huán)匹配 { citer = match[0].second; for (size_t i = 1; i < match.size(); ++i) { cout << match[i] << " "; } cout << endl; } return 0; }
regex_replace
替換匹配,即可以將符合匹配規(guī)則的子字符串替換為其他字符串。
將字符串中的-替換為/
#include <iostream> #include <regex> using namespace std; int main() { //替換不會修改原串 cout << regex_replace("2019-08-07", regex("-"), "/") << endl; return 0; }
匹配以逗號分隔的字符串(\S表示匹配任意顯示字符)
使用正則表達(dá)式將所有信息批處理為sql的語句
使用正則表達(dá)式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; }
將字符串中的/刪掉
總結(jié)
到此這篇關(guān)于C++11正則表達(dá)式(regex_match、regex_search和regex_replace)的文章就介紹到這了,更多相關(guān)C++11正則表達(dá)式內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
opengl實現(xiàn)直線掃描算法和區(qū)域填充算法
這篇文章主要為大家詳細(xì)介紹了opengl實現(xiàn)直線掃描算法和區(qū)域填充算法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-04-04C++11中std::function與std::bind的用法實例
大家都知道C++11中增加了許多的新特性,下面這篇文章主要給大家介紹了關(guān)于C++11中std::function與std::bind的用法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-05-05C語言中l(wèi)seek()函數(shù)和fseek()函數(shù)的使用詳解
這篇文章主要介紹了C語言中l(wèi)seek()函數(shù)和fseek()函數(shù)的使用詳解,是C語言入門學(xué)習(xí)中的基礎(chǔ)知識,需要的朋友可以參考下2015-08-08