如何获取未知数量的正则表达式匹配项
How to obtain an unknown number of regex matches?
我试图在一个字符串中找到几个数字的位置。我只能得到最后一个,或以前指定的位数:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string s("aaabbbccd123456eeffgg");
std::smatch match;
std::regex braced_regex("(\w+)(\d{2,})(\w+)");
std::regex plus_regex("(\w+)(\d+)(\w+)");
auto printer = [](auto& match) {
std::ssub_match sub(match);
std::string match_substring(sub.str());
std::cout << match_substring << 'n';
};
std::regex_match(s, match, braced_regex);
std::cout << "Number of braced matches: " << match.size() << 'n';
std::for_each(match.begin(), match.end(), printer);
std::regex_match(s, match, plus_regex);
std::cout << "Number of plus matches: " << match.size() << 'n';
std::for_each(match.begin(), match.end(), printer);
return 0;
}
结果:
Number of braced matches: 4
aaabbbccd123456eeffgg
aaabbbccd1234
56
eeffgg
Number of plus matches: 4
aaabbbccd123456eeffgg
aaabbbccd12345
6
eeffgg
如何从提供的字符串中获取整数序列,即123456?
([a-zA-Z]+)(\d{2,})([a-zA-Z]+)
你可以试试这个。w
=== [a-zA-Z0-9_]
.所以w+
会匹配它所能达到的最大目标。所以它只允许d{2,}
2。
或
(\w+?)(\d{2,})(\w+)
让第一个w
不贪婪。观看现场演示。
我认为
问题是数字被认为是单词部分并与w
匹配。我很想使用D
意思不是数字:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string s("aaabbbccd123456eeffgg");
std::smatch match;
std::regex plus_regex("(\D+)(\d+)(\D+)");
auto printer = [](auto& match) {
std::ssub_match sub(match);
std::string match_substring(sub.str());
std::cout << match_substring << 'n';
};
std::regex_match(s, match, plus_regex);
std::cout << "Number of plus matches: " << match.size() << 'n';
std::for_each(match.begin(), match.end(), printer);
return 0;
}
输出:
Number of plus matches: 4
aaabbbccd123456eeffgg
aaabbbccd
123456
eeffgg
另一种可能性(取决于你想要的)是使用std::regex_search()
它不会尝试匹配整个字符串,但允许您匹配中间的元素:
#include <iostream>
#include <regex>
#include <string>
int main()
{
std::string s("aaabbbccd123456eeffgg");
std::smatch match;
std::regex braced_regex("\d{2,}"); // just the numbers
auto printer = [](auto& match) {
std::ssub_match sub(match);
std::string match_substring(sub.str());
std::cout << match_substring << 'n';
};
std::regex_search(s, match, braced_regex); // NOTE: regex_search()!
std::cout << "Number of braced matches: " << match.size() << 'n';
std::for_each(match.begin(), match.end(), printer);
}
输出:
Number of braced matches: 1
123456
在:
(\w+)(\d{2,})(\w+)
\w+
匹配任何单词字符 [a-zA-Z0-9_],因此它也匹配 1234
将整数更改\w
与 [a-zA-Z_] 匹配,因此您将拥有:
std::regex braced_regex("([a-zA-Z_]+)(\d{2,})(\w+)");
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 组合正则表达式部分使用 | 不起作用的 C++
- 为什么C++正则表达式这么慢?
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- 在C++中实现正则表达式
- C++正则表达式替换整个单词
- 用C++编写正则表达式的正确方法是什么?
- 如何从Stroustrup的C++书中解释这个正则表达式?
- 为什么这个正则表达式C++在括号表达式中抛出无效范围异常?
- C++:匹配正则表达式,什么是匹配?
- C++:正则表达式匹配代码,打印多个匹配项?