跳到主要内容
版本:release

regex - 正则表达式插件

概述

DEPREDATED: 使用pkg: regex_pcre2替代

主要提供正则匹配/搜索/替换功能,接口和C++11标准库中保持一致。

功能使用说明

  1. 正则匹配,匹配字符串域传入的正则表达式,如果匹配则返回true,否则返回false(参照std::regex_match )
bool regex.match(string str, string rgx, RegexFlags flag = 0)
  1. 正则搜索,将所有匹配到的结果以字符串列表的形式返回(参照std::regex_search )
array regex.search(string str, string rgx, RegexFlags flag = 0)

注:当匹配到空的时候,固定向前移一个字符,否则会导致死循环。 比如:regex.search("abc", "[a]*")的匹配过程:

"abc" -> 匹配到"a"
"bc" -> 匹配到"",前移一个字符
"c" -> 匹配到"",前移一个字符
"" -> 匹配到"",匹配结束
  1. 正则替换(参照std::regex_replace )
string regex.replace(string str, string rgx, string fmt, RegexFlags flag)

举个例子:

// Match
bool is_match = false;
is_match = regex.match("subject", "(sub)(.*)", RegexFlags.DEFAULT); // is_match = true
is_match = regex.match("subject", "(sub)...."); // is_match = true
is_match = regex.match("subject", "sub.."); // is_match = flase

// Search
array search_ret = regex.search("this subject has a submarine as a subsequence", "\\b(sub)([^ ]*)");
//search_ret = [
// [ "subject", "sub", "ject", ],
// [ "submarine", "sub", "marine", ],
// [ "subsequence", "sub", "sequence", ],
// ];

// Replace
string new_str = regex.replace("this is a subsequence in the string", "\\b(sub)([^ ]*)", "sub-$2");
// new_str = "this is a sub-sequence in the string");

RegexFlags说明

gs中的枚举值c++标准库对应值说明
RegexFlags.DEFAULTstd::regex_constants::match_defaultDefault matching behavior. This constant has a value of zero**.
RegexFlags.NOT_BOLstd::regex_constants::match_not_bolThe first character is not considered a beginning of line ("^" does not match).
RegexFlags.NOT_EOLstd::regex_constants::match_not_eolThe last character is not considered an end of line ("$" does not match).
RegexFlags.NOT_BOWstd::regex_constants::match_not_bowThe escape sequence "\b" does not match as a beginning-of-word.
RegexFlags.NOT_EOWstd::regex_constants::match_not_eowThe escape sequence "\b" does not match as an end-of-word.
RegexFlags.ANYstd::regex_constants::match_anyAny match is acceptable if more than one match is possible.
RegexFlags.NOT_NULLstd::regex_constants::match_not_nullEmpty sequences do not match.
RegexFlags.CONTINUOUSstd::regex_constants::match_continuousThe expression must match a sub-sequence that begins at the first character. Sub-sequences must begin at the first character to match.
RegexFlags.PREV_AVAILstd::regex_constants::match_prev_availOne or more characters exist before the first one. (match_not_bol and match_not_bow are ignored)
RegexFlags.FORMAT_DEFAULTstd::regex_constants::format_defaultUses the standard formatting rules to replace matches (those used by ECMAScript's replace method)." "This constant has a value of zero**.
RegexFlags.FORMAT_SEDstd::regex_constants::format_sedUses the same rules as the sed utility in POSIX to replace matches.
RegexFlags.FORMAT_NO_COPYstd::regex_constants::format_no_copyThe sections in the target sequence that do not match the regular expression are not copied when replacing matches.
RegexFlags.FORMAT_FIRST_ONLYstd::regex_constants::format_first_onlyOnly the first occurrence of a regular expression is replaced.