1#ifndef NEFORCE_CORE_STRING_REGEX_HPP__
2#define NEFORCE_CORE_STRING_REGEX_HPP__
18NEFORCE_BEGIN_NAMESPACE__
30struct regex_exception final : value_exception {
31 explicit regex_exception(
const char* info =
"Regex Operation Failed",
const char*
type = static_type,
32 const int code = 0) noexcept :
35 explicit regex_exception(
const exception& e) :
38 ~regex_exception()
override =
default;
40 static constexpr auto static_type =
"regex_exception";
96 NEFORCE_NODISCARD
size_t position() const noexcept {
return position_; }
102 NEFORCE_NODISCARD
size_t length() const noexcept {
return length_; }
114 NEFORCE_NODISCARD
size_t size() const noexcept {
return groups_.size(); }
122 return idx < groups_.size() ? groups_[idx].view() :
"";
145 return matched() ? subject_.view(position_ + length_) :
"";
167 NEFORCE_NODISCARD
iterator begin() const noexcept {
return groups_.begin(); }
173 NEFORCE_NODISCARD
iterator end() const noexcept {
return groups_.end(); }
190 struct pcre2_code_deleter {
191 void operator()(::pcre2_code* code)
const noexcept {
192 if (code !=
nullptr) {
193 ::pcre2_code_free(code);
198 struct pcre2_match_data_deleter {
199 void operator()(::pcre2_match_data*
data)
const noexcept {
200 if (
data !=
nullptr) {
201 ::pcre2_match_data_free(
data);
209 int capture_count_ = 0;
211 friend class regex_iterator;
212 friend class regex_token_iterator;
217 match_result do_match(::PCRE2_SPTR subject,
size_t length,
size_t start_offset,
uint32_t options,
218 const string& subject_str)
const;
249 NEFORCE_NODISCARD
bool match(
const string& str)
const;
303 NEFORCE_NODISCARD
int capture_count() const noexcept {
return capture_count_; }
309 NEFORCE_NODISCARD
const string&
pattern() const noexcept {
return pattern_; }
315 NEFORCE_NODISCARD
bool valid() const noexcept {
return code_ !=
nullptr; }
322 NEFORCE_NODISCARD regex_iterator
begin(
const string& str)
const;
329 NEFORCE_NODISCARD regex_iterator
end(
const string& str)
const;
348 const regex* regex_ =
nullptr;
352 mutable bool cache_built_ =
false;
356 void build_cache()
const;
359 void move_previous();
361 void find_from_position(
size_t pos)
const;
362 void find_last_before_position(
size_t pos);
423 if (current_index_ == -1 && cache_built_) {
424 if (!cached_matches_.empty()) {
425 current_index_ =
static_cast<ptrdiff_t>(cached_matches_.size()) - 1;
477 it.current_index_ = -1;
504 const regex* regex_ =
nullptr;
510 state state_ = state::END;
511 size_t last_pos_ = 0;
575NEFORCE_END_NAMESPACE__
static constexpr size_type npos
match_result()=default
默认构造函数
vector< string >::const_iterator iterator
捕获组迭代器类型
iterator end() const noexcept
获取捕获组迭代器结束位置
string_view data() const noexcept
获取完整匹配的文本
size_t length() const noexcept
获取匹配长度
string_view operator[](const size_t idx) const noexcept
获取指定捕获组
match_result(string subject, size_t pos, size_t len, const vector< string > &groups, vector< pair< size_t, size_t > > group_positions)
构造函数
pair< size_t, size_t > position(const size_t idx) const noexcept
获取指定捕获组的位置信息
string_view prefix() const noexcept
获取匹配前的前缀
string_view suffix() const noexcept
获取匹配后的后缀
size_t position() const noexcept
获取匹配起始位置
iterator begin() const noexcept
格式化替换结果
bool matched() const noexcept
检查是否匹配成功
size_t size() const noexcept
获取捕获组数量
match_result value_type
元素类型
regex_iterator operator++(int)
后置递增操作符
bidirectional_iterator_tag iterator_category
迭代器类型
reference operator*() const
解引用操作符
bool operator==(const regex_iterator &other) const noexcept
相等比较操作符
regex_iterator()=default
默认构造函数,构造结束迭代器
static regex_iterator from_index(const regex *re, const string &str, ptrdiff_t index)
从指定索引构造迭代器
regex_iterator operator--(int)
后置递减操作符
ptrdiff_t difference_type
差值类型
static regex_iterator begin(const regex *re, const string &str)
获取起始迭代器
const match_result & reference
引用类型
regex_iterator(const regex *re, string str, size_t pos=0)
构造函数
regex_iterator & operator--()
前置递减操作符
bool operator!=(const regex_iterator &other) const noexcept
不等比较操作符
const match_result * pointer
指针类型
pointer operator->() const
成员访问操作符
regex_iterator & operator++()
前置递增操作符
static regex_iterator end(const regex *re, const string &str)
获取结束迭代器
bool operator!=(const regex_token_iterator &other) const noexcept
不等比较操作符
bool operator==(const regex_token_iterator &other) const noexcept
相等比较操作符
regex_token_iterator operator++(int)
后置递增操作符
regex_token_iterator(const regex *re, string str, int index=0)
构造函数
regex_token_iterator & operator++()
前置递增操作符
string_view operator*() const noexcept
解引用操作符
regex_token_iterator()=default
默认构造函数
match_result do_match(const string &str) const
执行完整匹配
regex_iterator end(const string &str) const
获取匹配结果迭代器结束位置
bool match(const string &str) const
检查是否完全匹配
int capture_count() const noexcept
获取捕获组数量
regex_iterator begin(const string &str) const
获取匹配结果迭代器起始位置
bool valid() const noexcept
检查正则表达式是否有效
regex(const string &pattern, uint32_t options=0)
从字符串构造正则表达式
string replace_first(const string &str, string_view fmt) const
替换第一个匹配
string replace_all_callback(const string &str, function< string(const match_result &)> callback) const
使用回调函数替换所有匹配
vector< string > split(const string &str, int max_splits=-1) const
使用正则表达式分割字符串
match_result search(const string &str, size_t pos=0) const
在字符串中搜索第一个匹配
string replace_all(const string &str, string_view fmt) const
替换所有匹配
vector< match_result > find_all(const string &str) const
查找所有匹配
const string & pattern() const noexcept
获取正则表达式模式
vector_iterator< true, vector< T, Alloc > > const_iterator
常量迭代器类型
unsigned int uint32_t
32位无符号整数类型
constexpr duration< inner::__common_rep_t< Rep1, Rep2 >, Period > operator*(const duration< Rep1, Period > &value, const Rep2 &scalar)
乘法运算符(持续时间 * 标量)
basic_string_view< char > string_view
字符字符串视图
constexpr decltype(auto) data(Container &cont) noexcept(noexcept(cont.data()))
获取容器的底层数据指针
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * type() const noexcept
获取异常类型
int code() const noexcept
获取异常码