std::basic_regex<CharT,Traits>::basic_regex

出自cppreference.com
< cpp‎ | regex‎ | basic regex
在2024年3月7日 (四) 06:21由Lynnboy對話 | 貢獻所做的修訂版本

(差異) ←上一修訂 | 最新修訂 (差異) | 下一修訂→ (差異)
 
 
 
正則表達式庫
(C++11)
算法
迭代器
異常
特徵
常量
(C++11)
正則表達式文法
 
 
basic_regex();
(1) (C++11 起)
explicit basic_regex( const CharT* s,
                      flag_type f = std::regex_constants::ECMAScript );
(2) (C++11 起)
basic_regex( const CharT* s, std::size_t count,
             flag_type f = std::regex_constants::ECMAScript );
(3) (C++11 起)
basic_regex( const basic_regex& other );
(4) (C++11 起)
basic_regex( basic_regex&& other ) noexcept;
(5) (C++11 起)
template< class ST, class SA >

explicit basic_regex( const std::basic_string<CharT,ST,SA>& str,

                      flag_type f = std::regex_constants::ECMAScript );
(6) (C++11 起)
template< class ForwardIt >

basic_regex( ForwardIt first, ForwardIt last,

             flag_type f = std::regex_constants::ECMAScript );
(7) (C++11 起)
basic_regex( std::initializer_list<CharT> init,
             flag_type f = std::regex_constants::ECMAScript );
(8) (C++11 起)

從按照標誌 f 進行解釋的字符序列構造新的正則表達式。

1) 默認構造函數。構造不匹配任何內容的空正則表達式。
2) 從空終止字符串 s 構造正則表達式。
3)s 所指向的 count 個的字符序列構造正則表達式。
4) 複製構造函數。通過複製 other 構造正則表達式。
5) 移動構造函數。用移動語義構造擁有 other 內容的正則表達式。
6) 從字符串 str 構造正則表達式。
7) 範圍構造函數。構造擁有範圍 [firstlast) 內容的正則表達式。
8) 初始化式列表構造函數。構造擁有初始化式列表 init 內容的正則表達式。

[編輯] 參數

s - 指向空終止字符串的指針
count - 用於初始化正則表達式的字符序列長度
first, last - 用於初始化正則表達式的字符序列範圍
str - 用作源初始化正則表達式的 basic_string
other - 用作源初始化正則表達式的另一正則表達式
init - 用於初始化正則表達式的初始化式列表
f - 用於指引轉譯字符序列為正則表達式的標誌
類型要求
-
ForwardIt 必須滿足老式向前迭代器 (LegacyForwardIterator)

[編輯] 異常

1) 可能會拋出由實現定義的異常。
2,3) 若提供的正則表達式非法則為 std::regex_error
4) 可能會拋出由實現定義的異常。
6-8) 若提供的正則表達式非法則為 std::regex_error

[編輯] 示例

#include <iomanip>
#include <iostream>
#include <regex>
#include <string>
 
void match_and_print(const std::string& text, const std::regex& pattern)
{
    std::sregex_iterator it(text.begin(), text.end(), pattern), it_end;
    int count = 0;
    for (; it != it_end; ++it)
    {
        const std::smatch& match = *it;
        std::cout << ++count << ". " << std::quoted(match.str()) << '\n';
    }
    std::cout << (count ? "\n" : "未找到匹配\n\n");
}
 
int main()
{
    const std::string text = "Hello, World! 12345";
 
    // 匹配一个或更多数字
    std::string pattern_text = "\\d+";
    std::cout << "数字 (" << pattern_text << "):\n";
    auto pattern = std::regex(pattern_text);
    match_and_print(text, pattern);
 
    // 匹配按空白分隔的一个或更多字符
    pattern_text = "[^\\s]+";
    std::cout << "单词 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text);
    match_and_print(text, pattern);
 
    // 匹配按空白分隔的一个或更多字符
    pattern_text = "[a-zA-Z]+";
    std::cout << "不带符号和数字的单词 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text);
    match_and_print(text, pattern);
 
    // 匹配一个非数字、非字母字符
    pattern_text = "[^0-9A-Za-z]";
    std::cout << "符号 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text);
    match_and_print(text, pattern);
 
    // 匹配一个或多个小写字母
    pattern_text = "[a-z]+";
    std::cout << "小写 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text);
    match_and_print(text, pattern);
 
    // 匹配一个或多个小写字母,带有 std::regex::icase 标志
    pattern_text = "[a-z]+";
    std::cout << "小写并带有忽略大小写标志 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text, std::regex::icase);
    match_and_print(text, pattern);
 
    // 匹配基本 POSIX 正则表达式 
    pattern_text = "[[:digit:]]+";
    std::cout << "基本 POSIX 正则表达式 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text, std::regex::basic);
    match_and_print(text, pattern);
 
    // 匹配扩展 POSIX 正则表达式
    pattern_text = "[[:digit:]]+";
    std::cout << "扩展 POSIX 正则表达式 (" << pattern_text << "):\n";
    pattern = std::regex(pattern_text, std::regex::extended);
    match_and_print(text, pattern);
}

輸出:

数字 (\d+):
1. "12345"
 
单词 ([^\s]+):
1. "Hello,"
2. "World!"
3. "12345"
 
不带符号和数字的单词 ([a-zA-Z]+):
1. "Hello"
2. "World"
 
符号 ([^0-9A-Za-z]):
1. ","
2. " "
3. "!"
4. " "
 
小写 ([a-z]+):
1. "ello"
2. "orld"
 
小写并带有忽略大小写标志 ([a-z]+):
1. "Hello"
2. "World"
 
基本 POSIX 正则表达式 ([[:digit:]]+):
未找到匹配
 
扩展 POSIX 正则表达式 ([[:digit:]]+):
1. "12345"