在运行时确定匹配参数的数量时,我无法使用RE2::FullMatchN。
const RE2::Arg* args[10];
int n;
bool match = RE2::FullMatchN("[email protected]", "([^ @]+)@([^ @]+)", args, n);
最后,我想在上面的示例中获得2个字符串-abcd和abcd.com
最佳答案
您可以将RE2
用作对象。如果RE2
对象已成功解析了正则表达式,则可以调用NumberOfCapturingGroups()
方法。知道有多少个捕获组,就可以动态地分配一个指向RE2::Arg
的指针数组。
这里是一个示例函数:
我还建议您将正则表达式包装在'('-')'中,因为默认情况下re2
不会返回完全匹配的0th
参数,这与许多其他API一样。
bool re2_full_match(const std::string & pattern, const std::string & str, std::vector<std::string> & results)
{
std::string wrapped_pattern = "(" + pattern + ")";
RE2::Options opt;
opt.set_log_errors(false);
opt.set_case_sensitive(false);
opt.set_utf8(false);
RE2 re2(wrapped_pattern, opt);
if (!re2.ok()) {
/// Failed to compile regular expression.
return false;
}
/// Argument vector.
std::vector<RE2::Arg> arguments;
/// Vercor of pointers to arguments.
std::vector<RE2::Arg *> arguments_ptrs;
/// Get number of arguments.
std::size_t args_count = re2.NumberOfCapturingGroups();
/// Adjust vectors sizes.
arguments.resize(args_count);
arguments_ptrs.resize(args_count);
results.resize(args_count);
/// Capture pointers to stack objects and result object in vector..
for (std::size_t i = 0; i < args_count; ++i) {
/// Bind argument to string from vector.
arguments[i] = &results[i];
/// Save pointer to argument.
arguments_ptrs[i] = &arguments[i];
}
return RE2::FullMatchN(StringPiece(str), re2, arguments_ptrs.data(), args_count);
}
但是本着正则表达式的精神,我建议您使用
^....$
而不是full_match
,并将full_match
重命名为find
:bool re2_find(const std::string & pattern, const std::string & str, std::vector<std::string> & results)
{
std::string wrapped_pattern = "(" + pattern + ")";
RE2::Options opt;
opt.set_log_errors(false);
opt.set_case_sensitive(false);
opt.set_utf8(false);
RE2 re2(wrapped_pattern, opt);
if (!re2.ok()) {
/// Failed to compile regular expression.
return false;
}
/// Argument vector.
std::vector<RE2::Arg> arguments;
/// Vercor of pointers to arguments.
std::vector<RE2::Arg *> arguments_ptrs;
/// Get number of arguments.
std::size_t args_count = re2.NumberOfCapturingGroups();
/// Adjust vectors sizes.
arguments.resize(args_count);
arguments_ptrs.resize(args_count);
results.resize(args_count);
/// Capture pointers to stack objects and result object in vector..
for (std::size_t i = 0; i < args_count; ++i) {
/// Bind argument to string from vector.
arguments[i] = &results[i];
/// Save pointer to argument.
arguments_ptrs[i] = &arguments[i];
}
StringPiece piece(str);
return RE2::FindAndConsumeN(&piece, re2, arguments_ptrs.data(), args_count);
}