47 template <
class CharT =
char,
class Traits = std::
char_traits<CharT>>
56 = std::match_results<typename string_type::const_iterator>;
58 using flag_type =
typename regex_type::flag_type;
61 = std::regex_iterator<
typename string_type::const_iterator,
63 std::regex_traits<CharT>>;
66 = std::regex_token_iterator<
typename string_type::const_iterator,
68 std::regex_traits<CharT>>;
109 for (
size_t i = 0; i < m.size(); ++i)
110 groups_.push_back({m.position(i), m.length(i)});
128 std::vector<string_type>
groups()
const {
129 std::vector<string_type> result;
131 for (
size_t i = 1; i <
groups_.size(); ++i)
142 int start(
int n = 0)
const {
152 int end(
int n = 0)
const {
162 std::pair<int, int>
span(
int n = 0)
const {
170 explicit operator bool()
const {
return success; }
181 if (std::regex_search(copy, m,
pattern))
182 return Match(std::move(copy), m);
194 if (std::regex_match(copy, m,
pattern))
195 return Match(std::move(copy), m);
212 std::vector<string_type> result;
215 for (
auto it = begin; it != end; ++it) result.push_back(it->str());
225 std::vector<string_type>
227 std::vector<string_type> result;
231 for (; it != end && (maxsplit <= 0 || count < maxsplit);
233 result.push_back(*it);
234 if (maxsplit > 0 && count == maxsplit && it != end)
235 result.push_back(
string_type((*it).first, text.end()));
248 int count = 0)
const {
249 if (count <= 0)
return std::regex_replace(text,
pattern, repl);
253 auto last = text.begin();
255 for (
auto it = begin; it != end && replaced < count;
257 result.append(last, text.begin() + it->position());
259 last = text.begin() + it->position() + it->length();
261 result.append(last, text.end());
274 int count = 0)
const {
276 auto result = std::regex_replace(text,
pattern, repl);
279 int cnt = int(std::distance(begin, end));
280 return {result, cnt};
285 auto last = text.begin();
287 for (
auto it = begin; it != end && replaced < count;
289 result.append(last, text.begin() + it->position());
291 last = text.begin() + it->position() + it->length();
293 result.append(last, text.end());
294 return {result, replaced};
309 std::regex::flag_type flags = std::regex::ECMAScript) {
310 return Regex(pattern, flags);
320 std::wregex::flag_type flags = std::wregex::ECMAScript) {
321 return WRegex(pattern, flags);
331 search(
const std::string &pattern,
const std::string &text) {
342 search(
const std::wstring &pattern,
const std::wstring &text) {
353 match(
const std::string &pattern,
const std::string &text) {
364 match(
const std::wstring &pattern,
const std::wstring &text) {
374 inline std::vector<std::string>
375 findall(
const std::string &pattern,
const std::string &text) {
385 inline std::vector<std::wstring>
386 findall(
const std::wstring &pattern,
const std::wstring &text) {
397 inline std::vector<std::string>
split(
const std::string &pattern,
398 const std::string &text,
410 inline std::vector<std::wstring>
split(
const std::wstring &pattern,
411 const std::wstring &text,
424 inline std::string
sub(
const std::string &pattern,
425 const std::string &repl,
426 const std::string &text,
428 return Regex(pattern).
sub(repl, text, count);
439 inline std::wstring
sub(
const std::wstring &pattern,
440 const std::wstring &repl,
441 const std::wstring &text,
443 return WRegex(pattern).
sub(repl, text, count);
451 inline std::string
escape(
const std::string &s) {
452 static std::regex special(R
"([.^$*+?()\[\]{}|\\])");
453 return std::regex_replace(s, special, R
"(\$&)");
461 inline std::wstring
escape(
const std::wstring &s) {
462 static std::wregex special(LR
"([.^$*+?()\[\]{}|\\])");
463 return std::regex_replace(s, special, LR
"(\$&)");
string_type text_copy
文本副本。
Definition re.h:85
bool success
匹配成功标志。
Definition re.h:91
int start(int n=0) const
获取指定组的起始位置。
Definition re.h:139
int end(int n=0) const
获取指定组的结束位置。
Definition re.h:149
std::vector< GroupInfo > groups_
组信息列表。
Definition re.h:90
Match()
默认构造函数,创建一个空的匹配结果。
Definition re.h:97
std::pair< int, int > span(int n=0) const
获取指定组的起始和结束位置。
Definition re.h:159
string_type group(int n=0) const
获取指定组的子串。
Definition re.h:115
std::vector< string_type > groups() const
获取所有组的子串。
Definition re.h:125
正则表达式对象,封装编译后的模式,提供匹配、搜索、替换等功能。
Definition re.h:48
Match fullmatch(const string_type &text) const
Definition re.h:201
std::basic_regex< CharT > regex_type
正则表达式类型。
Definition re.h:53
std::match_results< typename string_type::const_iterator > match_type
正则表达式的match_results类型。
Definition re.h:55
std::pair< string_type, int > subn(const string_type &repl, const string_type &text, int count=0) const
Definition re.h:269
regex_type pattern
Definition re.h:68
std::regex_token_iterator< typename string_type::const_iterator, CharT, std::regex_traits< CharT > > token_iterator
词元类型。
Definition re.h:63
Match search(string_type text) const
Definition re.h:175
std::vector< string_type > findall(const string_type &text) const
Definition re.h:208
typename regex_type::flag_type flag_type
标志类型。
Definition re.h:57
std::vector< string_type > split(const string_type &text, int maxsplit=0) const
Definition re.h:223
BasicRegex(const string_type &pat, flag_type flags=regex_type::ECMAScript)
构造函数,初始化正则表达式模式。
Definition re.h:76
std::basic_string< CharT, Traits > string_type
字符串类型。
Definition re.h:51
Match match(string_type text) const
Definition re.h:188
std::regex_iterator< typename string_type::const_iterator, CharT, std::regex_traits< CharT > > iterator_type
迭代器类型。
Definition re.h:59
string_type sub(const string_type &repl, const string_type &text, int count=0) const
Definition re.h:243
std::string escape(const std::string &s)
转义正则表达式中的特殊字符。
Definition re.h:451
std::vector< std::string > findall(const std::string &pattern, const std::string &text)
在文本中查找所有匹配正则表达式的子串。
Definition re.h:375
Regex::Match match(const std::string &pattern, const std::string &text)
在文本中匹配正则表达式的第一个匹配项。
Definition re.h:353
std::string sub(const std::string &pattern, const std::string &repl, const std::string &text, int count=0)
在文本中替换正则表达式匹配的子串。
Definition re.h:424
std::vector< std::string > split(const std::string &pattern, const std::string &text, int maxsplit=0)
在文本中按正则表达式模式分割字符串。
Definition re.h:397
Regex::Match search(const std::string &pattern, const std::string &text)
在文本中搜索正则表达式的第一个匹配项。
Definition re.h:331
Regex compile(const std::string &pattern, std::regex::flag_type flags=std::regex::ECMAScript)
编译字符串模式为正则表达式。
Definition re.h:308
BasicRegex< wchar_t > WRegex
宽字符正则表达式。
Definition re.h:299
BasicRegex< char > Regex
字符正则表达式。
Definition re.h:298
std::ptrdiff_t pos
组的起始位置。
Definition re.h:87
std::ptrdiff_t len
组的长度。
Definition re.h:88