詳解C++標(biāo)準(zhǔn)庫中處理正則表達式的類std::regex
std
是 C++ 標(biāo)準(zhǔn)庫的命名空間,包含了大量標(biāo)準(zhǔn)的 C++ 類、函數(shù)和對象。這些類和函數(shù)提供了廣泛的功能,包括輸入輸出、容器、算法、字符串處理等。
通常,為了使用標(biāo)準(zhǔn)庫中的對象和函數(shù),需在代碼中包含相應(yīng)的頭文件,比如 #include <iostream>
。然后你就可以通過 std::
前綴來使用其中的功能,比如 std::cout
、std::cin
、std::endl
等。
這種用法有助于防止命名沖突,因為 C++ 中可能會有多個庫提供相同的名稱。使用命名空間可以明確指定要使用的是標(biāo)準(zhǔn)庫中的功能,而不是其他地方定義的同名功能。
C++ 標(biāo)準(zhǔn)庫中常見的類、函數(shù)等:
1. 類:
- `std::string`: 字符串處理類。
- `std::vector`: 動態(tài)數(shù)組容器類。
- `std::map`、`std::unordered_map`: 鍵值對映射容器類。
- `std::fstream`: 文件輸入輸出類。
- `std::deque`: 雙端隊列容器類。
- `std::set`、`std::unordered_set`: 集合容器類。
- `std::stack`、`std::queue`: 棧和隊列容器適配器類。
- `std::stringstream`: 字符串流類。
2. 函數(shù):
- `std::cout`、`std::cerr`: 控制臺輸出函數(shù)。
- `std::cin`: 控制臺輸入函數(shù)。
- `std::sort`: 容器排序函數(shù)。
- `std::find`: 容器查找函數(shù)。
- `std::max`、`std::min`: 返回兩個值中較大或較小的值。
- `std::accumulate`: 容器元素累加函數(shù)。
- `std::copy`: 復(fù)制范圍內(nèi)元素到另一個范圍函數(shù)。
- `std::transform`: 容器元素轉(zhuǎn)換函數(shù)。
- `std::regex_search`: 正則表達式搜索函數(shù)。
- `std::regex_match`: 正則表達式匹配函數(shù)。
- `std::regex_replace`: 正則表達式替換函數(shù)。
3. 對象:
- `std::endl`: 換行并刷新輸出流對象。
- `std::numeric_limits`: 數(shù)值類型極限值信息對象。
- `std::allocator`: 動態(tài)內(nèi)存分配器對象。
- `std::cin.eof()`: 輸入流對象函數(shù),檢查是否達到文件結(jié)束。
- `std::nothrow`: 內(nèi)存分配失敗時返回空指針而不拋出異常的對象。
- `std::random_device`: 真隨機數(shù)生成對象。
- `std::locale`: 控制 C++ 標(biāo)準(zhǔn)庫本地化行為的對象。
這些類、函數(shù)和對象提供了豐富的功能,覆蓋了輸入輸出、容器、算法、字符串處理、正則表達式等多個方面,為 C++ 程序員提供了強大的工具,可用于各種類型的應(yīng)用開發(fā)。
------------
`std::regex` 是 C++ 標(biāo)準(zhǔn)庫中提供的用于處理正則表達式的類。正則表達式是一種強大的模式匹配工具,它可以用于在字符串中進行復(fù)雜的搜索、替換等操作。`std::regex` 類提供了一種方式來創(chuàng)建、編譯和使用正則表達式。
下面是 `std::regex` 類的一些重要成員函數(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` 對象,接受一個正則表達式字符串作為參數(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`
這兩個成員函數(shù)分別用于在字符串中進行完全匹配(`match()`)和部分匹配(`search()`)。它們接受一個待匹配的字符串作為參數(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)`
這個函數(shù)用于在范圍 `[first, last)` 中搜索并替換滿足正則表達式 `re` 的部分。替換的方式由參數(shù) `fmt` 指定。
4. 正則表達式的語法:
`std::regex` 支持多種正則表達式的語法,包括 ECMAScript、basic、extended 等等。你可以通過設(shè)置不同的標(biāo)志來指定使用的語法。常見的標(biāo)志包括:
- `std::regex_constants::ECMAScript`:使用 ECMAScript 語法。
- `std::regex_constants::basic`:使用基本正則表達式語法。
- `std::regex_constants::extended`:使用擴展正則表達式語法。
這些只是 `std::regex` 類的一些常用成員函數(shù)和用法。借助這些函數(shù),可方便地在字符串中進行正則表達式的搜索、替換等操作,實現(xiàn)了復(fù)雜文本處理的功能。
#include <iostream> #include <regex> #include <string> int main() { // 原始字符串 std::string text = "Hello, world!"; // 定義正則表達式模式 std::regex pattern("world"); // 在文本中搜索模式 if (std::regex_search(text, pattern)) { std::cout << "在文本中找到了匹配的模式!" << std::endl; } else { std::cout << "未找到匹配的模式!" << std::endl; } return 0; }
/*std::regex:表示一個正則表達式對象。
std::smatch:保存匹配結(jié)果的容器,可以通過 std::regex_match 或 std::regex_search 函數(shù)填充。
std::regex_match:用于檢查整個字符串是否與正則表達式匹配。
std::regex_search:在輸入字符串中搜索與正則表達式匹配的內(nèi)容。
std::regex_replace:用于在字符串中執(zhí)行正則表達式替換操作。
std::regex_iterator:用于迭代一個字符串中所有與正則表達式匹配的子串。
std::regex_token_iterator:用于迭代一個字符串中與正則表達式匹配的子串及其非匹配部分。*/
#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:表示一個正則表達式對象。 std::smatch:保存匹配結(jié)果的容器,可以通過 std::regex_match 或 std::regex_search 函數(shù)填充。 std::regex_match:用于檢查整個字符串是否與正則表達式匹配。 std::regex_search:在輸入字符串中搜索與正則表達式匹配的內(nèi)容。 std::regex_replace:用于在字符串中執(zhí)行正則表達式替換操作。 std::regex_iterator:用于迭代一個字符串中所有與正則表達式匹配的子串。 std::regex_token_iterator:用于迭代一個字符串中與正則表達式匹配的子串及其非匹配部分。*/
到此這篇關(guān)于C++標(biāo)準(zhǔn)庫中提供的用于處理正則表達式的類std::regex的文章就介紹到這了,更多相關(guān)C++標(biāo)準(zhǔn)庫正則表達式std::regex內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
strings命令分析淺談Go和C++編譯時的一點小區(qū)別
今天小編就為大家分享一篇關(guān)于strings命令分析淺談Go和C++編譯時的一點小區(qū)別,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04