(C++) 确定不匹配的正则表达式捕获组
(C++) Determine the unmatched regex capture groups
我想检查密码是否满足以下要求:
它应该:
- 至少包含 1 个小写字母 (ASCII 97-122)
- 至少包含 1 个字母 (65-90)
- 至少包含 1 位数字
- 至少包含 1 个特殊字符(33-47、58-64、91-96、123-126)
- 长度在 8 到 20 个字符之间
它还应该告诉我哪些要求不符合。
给定以下表达式,我可以使用库中std::regex
regex_match()对其进行验证
regex re("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!-/:-@[-`{-~])(\S){8,20}$");
但是这样我只能知道它是否匹配,因为它返回了一个boolean
鉴于此,在向表达式添加一些捕获组后,我尝试像下面这样迭代match_results
。
std::string str("AAAaaa111$$$");
std::regex rx("^((?=.*[a-z]).*)((?=.*[A-Z]).*)((?=.*[0-9]).*)((?=.*[!-/:-@[-`{-~]).*)(\S){8,20}$");
std::match_results< std::string::const_iterator > mr;
std::regex_search(str, mr, rx);
std::cout << "size: " << mr.size() << 'n'; // 6 or 0 only ?
for (int i = 0; i < mr.size(); i++) {
std::cout << "index: " << i << "t --> t" << mr.str(i) << endl;
}
if (regex_match(str, rx)) {
cout << "tests passed" << endl;
}
else {
cout << "tests failed" << endl;
}
它产生了以下输出:
size: 6
index: 0 --> AAAaaa111$$$
index: 1 --> AA
index: 2 --> Aa
index: 3 -->
index: 4 -->
index: 5 --> $
tests passed
Press any key to continue . . .
我想实现的是分辨出哪些组不匹配。例如,对于输入:SamplePassword1
只有第 4 组不匹配,因为它不包含特殊字符。然后,可以通知用户密码不符合哪些特定要求。因此,SamplePassword1$
将在每组中进行匹配并通过。
是否可以使用单个正则表达式而不是为每个要求使用单独的正则表达式来完成此任务?
我在这里遇到了一个类似的问题,但它在 C# .NET 中并使用命名捕获组。
var re = new Regex("((?<a>a)|(?<b>b))"); var ma = re.Match("a"); Console.WriteLine("a in a: " + ma.Groups["a"].Success); //true Console.WriteLine("b in a: " + ma.Groups["b"].Success); //false
std::regex rx("^((?=.*[a-z]))?((?=.*[A-Z]))?((?=.*[0-9]))?((?=.*[!-/:-@[-`{-~]))?(\S){8,20}$");
并与mr[i].first == str.end()
核实。演示
也就是说,回想一下这句话:
有些人在遇到问题时会想"我知道,我会使用正则表达式"。现在他们有两个问题。
相关文章:
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 查找中间两个数字的正则表达式的匹配项
- C++ 正则表达式不匹配空格
- 无法使用提升正则表达式查找匹配项
- 调试正则表达式不匹配
- 如何编写正则表达式以匹配以下数字格式?
- 为什么regex_match与我的正则表达式不匹配
- C++:矢量<string>与正则表达式不匹配
- 仅查找第一个 std::正则表达式有效匹配
- qt中的正则是不匹配字符串
- 提升正则表达式不匹配
- 正则表达式不允许在 dot(.) 之后立即写入 dot(.)
- Regex分组与C++11正则表达式库匹配
- 我的 XML 标签正则表达式不起作用
- 使用 libc++ 正则表达式库匹配"beginning-of-line" (C++11)
- C++11正则表达式不匹配任何内容
- 简单正则表达式不匹配
- Qt正则表达式不匹配
- 正则表达式IfThenElse -匹配单个闭括号或不匹配括号
- POSIX正则表达式不匹配