詳解C++標(biāo)準(zhǔn)庫(kù)中處理正則表達(dá)式的類(lèi)std::regex
std
是 C++ 標(biāo)準(zhǔn)庫(kù)的命名空間,包含了大量標(biāo)準(zhǔn)的 C++ 類(lèi)、函數(shù)和對(duì)象。這些類(lèi)和函數(shù)提供了廣泛的功能,包括輸入輸出、容器、算法、字符串處理等。
通常,為了使用標(biāo)準(zhǔn)庫(kù)中的對(duì)象和函數(shù),需在代碼中包含相應(yīng)的頭文件,比如 #include <iostream>
。然后你就可以通過(guò) std::
前綴來(lái)使用其中的功能,比如 std::cout
、std::cin
、std::endl
等。
這種用法有助于防止命名沖突,因?yàn)?C++ 中可能會(huì)有多個(gè)庫(kù)提供相同的名稱(chēng)。使用命名空間可以明確指定要使用的是標(biāo)準(zhǔn)庫(kù)中的功能,而不是其他地方定義的同名功能。
C++ 標(biāo)準(zhǔn)庫(kù)中常見(jiàn)的類(lèi)、函數(shù)等:
1. 類(lèi):
- `std::string`: 字符串處理類(lèi)。
- `std::vector`: 動(dòng)態(tài)數(shù)組容器類(lèi)。
- `std::map`、`std::unordered_map`: 鍵值對(duì)映射容器類(lèi)。
- `std::fstream`: 文件輸入輸出類(lèi)。
- `std::deque`: 雙端隊(duì)列容器類(lèi)。
- `std::set`、`std::unordered_set`: 集合容器類(lèi)。
- `std::stack`、`std::queue`: 棧和隊(duì)列容器適配器類(lèi)。
- `std::stringstream`: 字符串流類(lèi)。
2. 函數(shù):
- `std::cout`、`std::cerr`: 控制臺(tái)輸出函數(shù)。
- `std::cin`: 控制臺(tái)輸入函數(shù)。
- `std::sort`: 容器排序函數(shù)。
- `std::find`: 容器查找函數(shù)。
- `std::max`、`std::min`: 返回兩個(gè)值中較大或較小的值。
- `std::accumulate`: 容器元素累加函數(shù)。
- `std::copy`: 復(fù)制范圍內(nèi)元素到另一個(gè)范圍函數(shù)。
- `std::transform`: 容器元素轉(zhuǎn)換函數(shù)。
- `std::regex_search`: 正則表達(dá)式搜索函數(shù)。
- `std::regex_match`: 正則表達(dá)式匹配函數(shù)。
- `std::regex_replace`: 正則表達(dá)式替換函數(shù)。
3. 對(duì)象:
- `std::endl`: 換行并刷新輸出流對(duì)象。
- `std::numeric_limits`: 數(shù)值類(lèi)型極限值信息對(duì)象。
- `std::allocator`: 動(dòng)態(tài)內(nèi)存分配器對(duì)象。
- `std::cin.eof()`: 輸入流對(duì)象函數(shù),檢查是否達(dá)到文件結(jié)束。
- `std::nothrow`: 內(nèi)存分配失敗時(shí)返回空指針而不拋出異常的對(duì)象。
- `std::random_device`: 真隨機(jī)數(shù)生成對(duì)象。
- `std::locale`: 控制 C++ 標(biāo)準(zhǔn)庫(kù)本地化行為的對(duì)象。
這些類(lèi)、函數(shù)和對(duì)象提供了豐富的功能,覆蓋了輸入輸出、容器、算法、字符串處理、正則表達(dá)式等多個(gè)方面,為 C++ 程序員提供了強(qiáng)大的工具,可用于各種類(lèi)型的應(yīng)用開(kāi)發(fā)。
------------
`std::regex` 是 C++ 標(biāo)準(zhǔn)庫(kù)中提供的用于處理正則表達(dá)式的類(lèi)。正則表達(dá)式是一種強(qiáng)大的模式匹配工具,它可以用于在字符串中進(jìn)行復(fù)雜的搜索、替換等操作。`std::regex` 類(lèi)提供了一種方式來(lái)創(chuàng)建、編譯和使用正則表達(dá)式。
下面是 `std::regex` 類(lèi)的一些重要成員函數(shù)和用法:
1. 構(gòu)造函數(shù):
- `explicit regex(const char* fmt, flag_type flags = std::regex_constants::ECMAScript)`
- `explicit regex(const std::string& fmt, flag_type flags = std::regex_constants::ECMAScript)`
這些構(gòu)造函數(shù)用于創(chuàng)建 `std::regex` 對(duì)象,接受一個(gè)正則表達(dá)式字符串作為參數(shù),并可選擇地指定匹配標(biāo)志。
2. 成員函數(shù) `match()` 和 `search()`:
- `bool match(const std::string& s, std::regex_constants::match_flag_type flags = std::regex_constants::match_default) const`
- `bool search(const std::string& s, std::regex_constants::match_flag_type flags = std::regex_constants::match_default) const`
這兩個(gè)成員函數(shù)分別用于在字符串中進(jìn)行完全匹配(`match()`)和部分匹配(`search()`)。它們接受一個(gè)待匹配的字符串作為參數(shù),并可選擇地指定匹配標(biāo)志。
3. 替換函數(shù) `std::regex_replace()`:
- `std::string regex_replace(InputIt first, InputIt last, const std::regex& re, const std::string& fmt, std::regex_constants::match_flag_type flags = std::regex_constants::match_default)`
這個(gè)函數(shù)用于在范圍 `[first, last)` 中搜索并替換滿(mǎn)足正則表達(dá)式 `re` 的部分。替換的方式由參數(shù) `fmt` 指定。
4. 正則表達(dá)式的語(yǔ)法:
`std::regex` 支持多種正則表達(dá)式的語(yǔ)法,包括 ECMAScript、basic、extended 等等。你可以通過(guò)設(shè)置不同的標(biāo)志來(lái)指定使用的語(yǔ)法。常見(jiàn)的標(biāo)志包括:
- `std::regex_constants::ECMAScript`:使用 ECMAScript 語(yǔ)法。
- `std::regex_constants::basic`:使用基本正則表達(dá)式語(yǔ)法。
- `std::regex_constants::extended`:使用擴(kuò)展正則表達(dá)式語(yǔ)法。
這些只是 `std::regex` 類(lèi)的一些常用成員函數(shù)和用法。借助這些函數(shù),可方便地在字符串中進(jìn)行正則表達(dá)式的搜索、替換等操作,實(shí)現(xiàn)了復(fù)雜文本處理的功能。
#include <iostream> #include <regex> #include <string> int main() { // 原始字符串 std::string text = "Hello, world!"; // 定義正則表達(dá)式模式 std::regex pattern("world"); // 在文本中搜索模式 if (std::regex_search(text, pattern)) { std::cout << "在文本中找到了匹配的模式!" << std::endl; } else { std::cout << "未找到匹配的模式!" << std::endl; } return 0; }
/*std::regex:表示一個(gè)正則表達(dá)式對(duì)象。
std::smatch:保存匹配結(jié)果的容器,可以通過(guò) std::regex_match 或 std::regex_search 函數(shù)填充。
std::regex_match:用于檢查整個(gè)字符串是否與正則表達(dá)式匹配。
std::regex_search:在輸入字符串中搜索與正則表達(dá)式匹配的內(nèi)容。
std::regex_replace:用于在字符串中執(zhí)行正則表達(dá)式替換操作。
std::regex_iterator:用于迭代一個(gè)字符串中所有與正則表達(dá)式匹配的子串。
std::regex_token_iterator:用于迭代一個(gè)字符串中與正則表達(dá)式匹配的子串及其非匹配部分。*/
#include <iostream> #include <regex> int main() { std::string text = "Hello, my email is example@email.com and my phone number is 123-456-7890."; std::regex emailRegex("[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}"); std::regex phoneRegex("\\d{3}-\\d{3}-\\d{4}"); // Match email std::smatch emailMatch; if (std::regex_search(text, emailMatch, emailRegex)) { std::cout << "Email found: " << emailMatch.str() << std::endl; } // Match phone number std::smatch phoneMatch; if (std::regex_search(text, phoneMatch, phoneRegex)) { std::cout << "Phone number found: " << phoneMatch.str() << std::endl; } // Replace phone number std::string newText = std::regex_replace(text, phoneRegex, "XXX-XXX-XXXX"); std::cout << "Text with phone number replaced: " << newText << std::endl; return 0; } /*std::regex:表示一個(gè)正則表達(dá)式對(duì)象。 std::smatch:保存匹配結(jié)果的容器,可以通過(guò) std::regex_match 或 std::regex_search 函數(shù)填充。 std::regex_match:用于檢查整個(gè)字符串是否與正則表達(dá)式匹配。 std::regex_search:在輸入字符串中搜索與正則表達(dá)式匹配的內(nèi)容。 std::regex_replace:用于在字符串中執(zhí)行正則表達(dá)式替換操作。 std::regex_iterator:用于迭代一個(gè)字符串中所有與正則表達(dá)式匹配的子串。 std::regex_token_iterator:用于迭代一個(gè)字符串中與正則表達(dá)式匹配的子串及其非匹配部分。*/
到此這篇關(guān)于C++標(biāo)準(zhǔn)庫(kù)中提供的用于處理正則表達(dá)式的類(lèi)std::regex的文章就介紹到這了,更多相關(guān)C++標(biāo)準(zhǔn)庫(kù)正則表達(dá)式std::regex內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語(yǔ)言實(shí)現(xiàn)共享單車(chē)管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)共享單車(chē)管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C語(yǔ)言實(shí)現(xiàn)員工工資管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言實(shí)現(xiàn)員工工資管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02面試常見(jiàn)問(wèn)題之C語(yǔ)言與C++的區(qū)別問(wèn)題
在C中,用static修飾的變量或函數(shù),主要用來(lái)說(shuō)明這個(gè)變量或函數(shù)只能在本文件代碼塊中訪(fǎng)問(wèn),而文件外部的代碼無(wú)權(quán)訪(fǎng)問(wèn),今天重點(diǎn)給大家介紹面試中常見(jiàn)的C語(yǔ)言與C++區(qū)別的問(wèn)題,感興趣的朋友跟隨小編一起看看吧2021-05-05strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別
今天小編就為大家分享一篇關(guān)于strings命令分析淺談Go和C++編譯時(shí)的一點(diǎn)小區(qū)別,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04C++實(shí)現(xiàn)FTP綜合應(yīng)用詳解
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)FTP綜合應(yīng)用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08