56 std::regex::flag_type flags = std::regex::ECMAScript)
57 : pattern(pat, flags) {}
75 Match(
const std::smatch &m) : match(m), success(true) {}
82 std::string
group(
int n = 0)
const
84 if (!success || n < 0 || n >=
int(match.size()))
88 return match[n].str();
95 std::vector<std::string>
groups()
const
97 std::vector<std::string> result;
100 for (
size_t i = 1; i < match.size(); ++i)
102 result.push_back(match[i].str());
114 if (!success || n < 0 || n >=
int(match.size()))
118 return match.position(n);
128 if (!success || n < 0 || n >=
int(match.size()))
132 return match.position(n) + match.length(n);
140 std::pair<int, int>
span(
int n = 0)
const
149 explicit operator bool()
const {
return success; }
160 if (std::regex_search(text,
match, pattern))
175 if (std::regex_match(text,
match, pattern))
197 std::vector<std::string>
findall(
const std::string &text)
const
199 std::vector<std::string> result;
200 auto begin = std::sregex_iterator(
201 text.begin(), text.end(), pattern);
202 auto end = std::sregex_iterator();
204 for (
auto it = begin; it != end; ++it)
206 result.push_back(it->str());
218 std::sregex_iterator it;
253 return {{std::sregex_iterator(text.begin(), text.end(), pattern)},
254 {std::sregex_iterator()}};
263 std::vector<std::string>
split(
const std::string &text,
264 int maxsplit = 0)
const
266 std::vector<std::string> result;
267 std::sregex_token_iterator it(text.begin(), text.end(), pattern, -1);
268 std::sregex_token_iterator end;
271 for (; it != end && (maxsplit <= 0 || count < maxsplit);
274 result.push_back(*it);
277 if (maxsplit > 0 && count == maxsplit && it != end)
279 std::string remaining;
280 for (; it != end; ++it)
282 if (!remaining.empty())
287 result.push_back(remaining);
300 std::string
sub(
const std::string &repl,
301 const std::string &text,
int count = 0)
const
305 return std::regex_replace(text, pattern, repl);
309 auto begin = std::sregex_iterator(
310 text.begin(), text.end(), pattern);
311 auto end = std::sregex_iterator();
312 auto last = text.begin();
315 for (
auto it = begin;
316 it != end && replaced < count;
319 result.append(last, text.begin() + it->position());
321 last = text.begin() + it->position() + it->length();
323 result.append(last, text.end());
335 std::pair<std::string, int>
subn(
const std::string &repl,
336 const std::string &text,
341 auto result = std::regex_replace(text, pattern, repl);
342 auto begin = std::sregex_iterator(
343 text.begin(), text.end(), pattern);
344 auto end = std::sregex_iterator();
345 int cnt = std::distance(begin, end);
346 return {result, cnt};
350 auto begin = std::sregex_iterator(
351 text.begin(), text.end(), pattern);
352 auto end = std::sregex_iterator();
353 auto last = text.begin();
356 for (
auto it = begin;
357 it != end && replaced < count;
360 result.append(last, text.begin() + it->position());
362 last = text.begin() + it->position() + it->length();
364 result.append(last, text.end());
366 return {result, replaced};
383 std::regex::flag_type flags = std::regex::ECMAScript)
385 return Regex(pattern, flags);
395 const std::string &text)
407 const std::string &text)
418 inline std::vector<std::string>
findall(
const std::string &pattern,
419 const std::string &text)
431 inline std::vector<std::string>
split(
const std::string &pattern,
432 const std::string &text,
446 inline std::string
sub(
const std::string &pattern,
447 const std::string &repl,
448 const std::string &text,
int count = 0)
450 return Regex(pattern).
sub(repl, text, count);
458 inline std::string
escape(
const std::string &s)
460 static std::regex special(R
"([.^$*+?()\[\]{}|\\])");
461 return std::regex_replace(s, special, R
"(\$&)");
迭代器,用于遍历所有匹配。
Definition re.h:216
Iterator(std::sregex_iterator i)
Definition re.h:221
bool operator!=(const Iterator &other) const
Definition re.h:224
bool operator==(const Iterator &other) const
Definition re.h:223
void operator++()
Definition re.h:225
Match operator*() const
Definition re.h:226
包装起始和结束迭代器,支持范围 for 循环。
Definition re.h:234
IteratorPair(iterator beg, iterator end)
Definition re.h:241
iterator begin() const
Definition re.h:242
iterator end() const
Definition re.h:243
Iterator iterator
Definition re.h:240
匹配结果对象,包含匹配信息。
Definition re.h:65
int start(int n=0) const
获取指定捕获组在原始字符串中的起始索引。
Definition re.h:112
Match()
默认构造,表示不成功的匹配。
Definition re.h:72
int end(int n=0) const
获取指定捕获组在原始字符串中的结束索引(最后一个字符之后的位置)。
Definition re.h:126
std::vector< std::string > groups() const
获取所有捕获组(不包括整个匹配)的内容。
Definition re.h:95
std::pair< int, int > span(int n=0) const
获取指定捕获组的起始和结束索引组成的 pair。
Definition re.h:140
Match(const std::smatch &m)
从 std::smatch 构造。
Definition re.h:75
std::string group(int n=0) const
获取指定捕获组的内容。
Definition re.h:82
正则表达式对象,封装编译后的模式,提供匹配、搜索、替换等功能。
Definition re.h:45
Match search(const std::string &text) const
在文本中搜索第一个匹配。
Definition re.h:157
std::pair< std::string, int > subn(const std::string &repl, const std::string &text, int count=0) const
替换匹配的子串并返回替换次数。
Definition re.h:335
std::vector< std::string > findall(const std::string &text) const
查找所有不重叠的匹配,返回匹配字符串列表。
Definition re.h:197
Regex(const std::string &pat, std::regex::flag_type flags=std::regex::ECMAScript)
构造一个 Regex 对象。
Definition re.h:55
Match fullmatch(const std::string &text) const
match 的别名,与 Python 的 re.fullmatch 语义相同。
Definition re.h:187
std::vector< std::string > split(const std::string &text, int maxsplit=0) const
使用正则表达式分割字符串。
Definition re.h:263
Match match(const std::string &text) const
尝试从文本开头匹配整个模式。
Definition re.h:172
std::string sub(const std::string &repl, const std::string &text, int count=0) const
替换匹配的子串(最多 count 次)。
Definition re.h:300
IteratorPair finditer(const std::string &text) const
返回一个可迭代对象,遍历所有匹配的 Match 对象。
Definition re.h:251
std::string escape(const std::string &s)
转义正则表达式中的特殊字符。
Definition re.h:458
std::vector< std::string > findall(const std::string &pattern, const std::string &text)
查找所有匹配的字符串(函数式接口)。
Definition re.h:418
Regex::Match match(const std::string &pattern, const std::string &text)
从开头匹配(函数式接口)。
Definition re.h:406
std::string sub(const std::string &pattern, const std::string &repl, const std::string &text, int count=0)
替换匹配的子串(函数式接口)。
Definition re.h:446
std::vector< std::string > split(const std::string &pattern, const std::string &text, int maxsplit=0)
分割字符串(函数式接口)。
Definition re.h:431
Regex::Match search(const std::string &pattern, const std::string &text)
搜索第一个匹配(函数式接口)。
Definition re.h:394
Regex compile(const std::string &pattern, std::regex::flag_type flags=std::regex::ECMAScript)
编译正则表达式并返回 Regex 对象。
Definition re.h:382
函数式正则表达式接口,模仿 Python 的 re 模块。