“cpp/regex/basic regex/basic regex”的版本间的差异
来自cppreference.com
< cpp | regex | basic regex
小 |
|||
第1行: | 第1行: | ||
− | {{cpp/regex/basic_regex/title | basic_regex}} | + | {{cpp/regex/basic_regex/title|basic_regex}} |
{{cpp/regex/basic_regex/navbar}} | {{cpp/regex/basic_regex/navbar}} | ||
{{dcl begin}} | {{dcl begin}} | ||
− | {{dcl | num=1 | since=c++11 | 1= | + | {{dcl|num=1|since=c++11|1= |
basic_regex(); | basic_regex(); | ||
}} | }} | ||
− | {{dcl | num=2 | since=c++11 | 1= | + | {{dcl|num=2|since=c++11|1= |
− | explicit basic_regex( const CharT* s, | + | explicit basic_regex( const CharT* s, |
flag_type f = std::regex_constants::ECMAScript ); | flag_type f = std::regex_constants::ECMAScript ); | ||
}} | }} | ||
− | {{dcl | num=3 | since=c++11 | 1= | + | {{dcl|num=3|since=c++11|1= |
basic_regex( const CharT* s, std::size_t count, | basic_regex( const CharT* s, std::size_t count, | ||
flag_type f = std::regex_constants::ECMAScript ); | flag_type f = std::regex_constants::ECMAScript ); | ||
}} | }} | ||
− | {{dcl | num=4 | since=c++11 | 1= | + | {{dcl|num=4|since=c++11|1= |
basic_regex( const basic_regex& other ); | basic_regex( const basic_regex& other ); | ||
}} | }} | ||
− | {{dcl | num=5 | since=c++11 | 1= | + | {{dcl|num=5|since=c++11|1= |
basic_regex( basic_regex&& other ) noexcept; | basic_regex( basic_regex&& other ) noexcept; | ||
}} | }} | ||
− | {{dcl | num=6 | since=c++11 | 1= | + | {{dcl|num=6|since=c++11|1= |
template< class ST, class SA > | template< class ST, class SA > | ||
− | explicit basic_regex( const std::basic_string<CharT,ST,SA>& str, | + | explicit basic_regex( const std::basic_string<CharT,ST,SA>& str, |
flag_type f = std::regex_constants::ECMAScript ); | flag_type f = std::regex_constants::ECMAScript ); | ||
}} | }} | ||
− | {{dcl | num=7 | since=c++11 | 1= | + | {{dcl|num=7|since=c++11|1= |
template< class ForwardIt > | template< class ForwardIt > | ||
− | basic_regex( ForwardIt first, ForwardIt last, | + | basic_regex( ForwardIt first, ForwardIt last, |
flag_type f = std::regex_constants::ECMAScript ); | flag_type f = std::regex_constants::ECMAScript ); | ||
}} | }} | ||
− | {{dcl | num=8 | since=c++11 | 1= | + | {{dcl|num=8|since=c++11|1= |
− | basic_regex( std::initializer_list<CharT> init, | + | basic_regex( std::initializer_list<CharT> init, |
flag_type f = std::regex_constants::ECMAScript ); | flag_type f = std::regex_constants::ECMAScript ); | ||
}} | }} | ||
{{dcl end}} | {{dcl end}} | ||
− | 从按照标志 {{ | + | 从按照标志 {{|f}} 的字符序列构造新的正则表达式。 |
− | @1@默认构造函数。构造 | + | @1@ 默认构造函数。构造不匹配 内容的空正则表达式。 |
− | @2@从空终止字符串 {{ | + | @2@ 从空终止字符串 {{|s}} 构造正则表达式。 |
− | @3@从 {{ | + | @3@ 从 {{|s}} 所指向的 {{|count}} 个的字符序列构造正则表达式。 |
− | @4@复制构造函数。通过复制 {{ | + | @4@ 复制构造函数。通过复制 {{|other}} 构造正则表达式。 |
− | @5@移动构造函数。用移动语义构造拥有 {{ | + | @5@ 移动构造函数。用移动语义构造拥有 {{|other}} 内容的正则表达式。 |
− | @6@从 | + | @6@ 从 {{|str}} 构造正则表达式。 |
− | @7@范围构造函数。构造拥有范围 {{ | + | @7@ 范围构造函数。构造拥有范围 {{|firstlast}} 内容的正则表达式。 |
− | @8@ | + | @8@ 构造函数。构造拥有 {{|init}} 内容的正则表达式。 |
===参数=== | ===参数=== | ||
{{par begin}} | {{par begin}} | ||
− | {{par | s |指向空终止字符串的指针}} | + | {{par|s|指向空终止字符串的指针}} |
− | {{par | count |用于初始化 | + | {{par|count|用于初始化 的字符序列长度}} |
− | {{par | first, last |用于初始化 | + | {{par|first, last|用于初始化 的字符序列范围}} |
− | {{par | str |用作源初始化 | + | {{par|str|用作源初始化 的 basic_string}} |
− | {{par | other |用作源初始化 | + | {{par|other|用作源初始化 的另一 }} |
− | {{par | init |用于初始化 | + | {{par|init|用于初始化 的 }} |
− | {{par | f |用于指引转译字符序列为正则表达式的标志}} | + | {{par|f|用于指引转译字符序列为正则表达式的标志}} |
{{par hreq}} | {{par hreq}} | ||
− | {{par req named | ForwardIt |ForwardIterator}} | + | {{par req named|ForwardIt|ForwardIterator}} |
{{par end}} | {{par end}} | ||
===异常=== | ===异常=== | ||
− | @1@{{cpp/impldef exception item}} | + | @1@ {{cpp/impldef exception item}} |
− | @2 | + | @23@ 若提供的正则表达式非法则为 {{lc|std::regex_error}}。 |
− | @4@{{cpp/impldef exception item}} | + | @4@ {{cpp/impldef exception item}} |
− | @6-8@若提供的正则表达式非法则为 {{lc|std::regex_error}} | + | @6-8@ 若提供的正则表达式非法则为 {{lc|std::regex_error}}。 |
===示例=== | ===示例=== | ||
− | {{example}} | + | {{example |
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | }} | ||
{{langlinks|de|en|es|fr|it|ja|pt|ru}} | {{langlinks|de|en|es|fr|it|ja|pt|ru}} |
2024年3月7日 (四) 06:21的最后版本
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, |
(6) | (C++11 起) |
template< class ForwardIt > basic_regex( ForwardIt first, ForwardIt last, |
(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) 范围构造函数。构造拥有范围
[
first,
last)
内容的正则表达式。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"