Console Library 8.0.0
A header-only library that makes C++ simple
Loading...
Searching...
No Matches
console::BasicRegex< CharT, Traits > Class Template Reference

正则表达式对象,封装编译后的模式,提供匹配、搜索、替换等功能。 More...

#include <re.h>

Classes

class  Match
 匹配结果对象,保存文本副本和位置信息,不依赖迭代器有效性。 More...

Public Types

using string_type = std::basic_string<CharT, Traits>
 字符串类型。
using regex_type = std::basic_regex<CharT>
 正则表达式类型。
using match_type = std::match_results<typename string_type::const_iterator>
 正则表达式的match_results类型。
using flag_type = typename regex_type::flag_type
 标志类型。
using iterator_type
 迭代器类型。
using token_iterator
 词元类型。

Public Member Functions

 BasicRegex (const string_type &pat, flag_type flags=regex_type::ECMAScript)
 构造函数,初始化正则表达式模式。
Match search (string_type text) const
 搜索第一个匹配。
Match match (string_type text) const
 完全匹配整个文本。
Match fullmatch (const string_type &text) const
 完全匹配整个文本。
std::vector< string_type > findall (const string_type &text) const
 查找所有不重叠的匹配。
std::vector< string_type > split (const string_type &text, int maxsplit=0) const
 使用正则表达式分割字符串。
string_type sub (const string_type &repl, const string_type &text, int count=0) const
 替换匹配的子串。
std::pair< string_type, int > subn (const string_type &repl, const string_type &text, int count=0) const
 替换匹配的子串并返回替换次数。

Private Attributes

regex_type pattern
 正则表达式模式。

Detailed Description

template<class CharT = char, class Traits = std::char_traits<CharT>>
class console::BasicRegex< CharT, Traits >

正则表达式对象,封装编译后的模式,提供匹配、搜索、替换等功能。

Template Parameters
CharT字符类型(char 或 wchar_t)。
Traits字符特征类型。

Member Typedef Documentation

◆ flag_type

template<class CharT = char, class Traits = std::char_traits<CharT>>
using console::BasicRegex< CharT, Traits >::flag_type = typename regex_type::flag_type

标志类型。

◆ iterator_type

template<class CharT = char, class Traits = std::char_traits<CharT>>
using console::BasicRegex< CharT, Traits >::iterator_type
Initial value:
std::regex_iterator<typename string_type::const_iterator,
CharT,
std::regex_traits<CharT>>

迭代器类型。

◆ match_type

template<class CharT = char, class Traits = std::char_traits<CharT>>
using console::BasicRegex< CharT, Traits >::match_type = std::match_results<typename string_type::const_iterator>

正则表达式的match_results类型。

◆ regex_type

template<class CharT = char, class Traits = std::char_traits<CharT>>
using console::BasicRegex< CharT, Traits >::regex_type = std::basic_regex<CharT>

正则表达式类型。

◆ string_type

template<class CharT = char, class Traits = std::char_traits<CharT>>
using console::BasicRegex< CharT, Traits >::string_type = std::basic_string<CharT, Traits>

字符串类型。

◆ token_iterator

template<class CharT = char, class Traits = std::char_traits<CharT>>
using console::BasicRegex< CharT, Traits >::token_iterator
Initial value:
std::regex_token_iterator<typename string_type::const_iterator,
CharT,
std::regex_traits<CharT>>

词元类型。

Constructor & Destructor Documentation

◆ BasicRegex()

template<class CharT = char, class Traits = std::char_traits<CharT>>
console::BasicRegex< CharT, Traits >::BasicRegex ( const string_type & pat,
flag_type flags = regex_type::ECMAScript )
inline

构造函数,初始化正则表达式模式。

Parameters
pat正则表达式模式字符串。
flags正则表达式标志,默认为ECMAScript。

Member Function Documentation

◆ findall()

template<class CharT = char, class Traits = std::char_traits<CharT>>
std::vector< string_type > console::BasicRegex< CharT, Traits >::findall ( const string_type & text) const
inline

查找所有不重叠的匹配。

Parameters
text要搜索的文本。
Returns
匹配成功时返回匹配对象列表,否则返回空列表。

◆ fullmatch()

template<class CharT = char, class Traits = std::char_traits<CharT>>
Match console::BasicRegex< CharT, Traits >::fullmatch ( const string_type & text) const
inline

完全匹配整个文本。

Parameters
text要匹配的文本。
Returns
匹配成功时返回匹配对象,否则返回空对象。

◆ match()

template<class CharT = char, class Traits = std::char_traits<CharT>>
Match console::BasicRegex< CharT, Traits >::match ( string_type text) const
inline

完全匹配整个文本。

Parameters
text要匹配的文本。
Returns
匹配成功时返回匹配对象,否则返回空对象。

◆ search()

template<class CharT = char, class Traits = std::char_traits<CharT>>
Match console::BasicRegex< CharT, Traits >::search ( string_type text) const
inline

搜索第一个匹配。

Parameters
text要搜索的文本。
Returns
匹配成功时返回匹配对象,否则返回空对象。

◆ split()

template<class CharT = char, class Traits = std::char_traits<CharT>>
std::vector< string_type > console::BasicRegex< CharT, Traits >::split ( const string_type & text,
int maxsplit = 0 ) const
inline

使用正则表达式分割字符串。

Parameters
text要分割的文本。
maxsplit最大分割次数,默认为 0 表示不限制。
Returns
分割后的字符串列表。

◆ sub()

template<class CharT = char, class Traits = std::char_traits<CharT>>
string_type console::BasicRegex< CharT, Traits >::sub ( const string_type & repl,
const string_type & text,
int count = 0 ) const
inline

替换匹配的子串。

Parameters
repl替换字符串。
text要替换的文本。
count替换次数,默认为 0 表示替换所有匹配。
Returns
替换后的字符串。

◆ subn()

template<class CharT = char, class Traits = std::char_traits<CharT>>
std::pair< string_type, int > console::BasicRegex< CharT, Traits >::subn ( const string_type & repl,
const string_type & text,
int count = 0 ) const
inline

替换匹配的子串并返回替换次数。

Parameters
repl替换字符串。
text要替换的文本。
count替换次数,默认为 0 表示替换所有匹配。
Returns
替换后的字符串和替换次数的对。

Member Data Documentation

◆ pattern

template<class CharT = char, class Traits = std::char_traits<CharT>>
regex_type console::BasicRegex< CharT, Traits >::pattern
private

正则表达式模式。


The documentation for this class was generated from the following file: