Namespaces
Variants

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

From cppreference.net
Regular expressions library
Classes
(C++11)
Algorithms
Iterators
Exceptions
Traits
Constants
(C++11)
Regex Grammar
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) 范围构造函数。使用范围 [ 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" : "no match found\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"