如何让正则表达式匹配句子中间?
How to have regex to match middle of sentence?
我试图让正则表达式匹配字符串上的某些内容,然后捕获字符串的其余部分。根据 https://regex101.com/下面的正则表达式应该可以工作,但事实并非如此。似乎 c++ 正则表达式只会从字符串的开头匹配。如何让它从字符串的中间开始?我知道我可以.*?/word(.*)
但我宁愿不这样做,因为正则表达式是用户输入......
std::string uri("a/word/");
std::smatch match;
std::regex rgx(R"(/word(.*))");
if (std::regex_match(uri, match, rgx)) {
std::cout << match[0] << ' ' << match[1];
}
regex_match
应该匹配整个输入序列,而不是其中的一部分。对于后者,您需要regex_search
.
使用它,您的示例打印/word/ /
.
现场演示
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何删除peer if else分支中的冗长句子
- 递归形成字符串中所有数字字符的中间和?
- 链表错误的中间
- 在输入句子时终止 std::out_of_range
- 使用 C++ 验证 NMEA 句子
- 分析包含 NMEA 句子的日志文件C++
- 使(虚拟)函数在大多数派生类中无法访问中间基类中可访问,定义良好?
- 从函数返回范围视图时,带有std::span:中间对象所有权的C++Ranges-v3
- 如何输入数组的元素,每次获得新元素时,我们将其放在数组的中间?
- 查找中间两个数字的正则表达式的匹配项
- Vscode/C++ - cout 无法输出完整的句子,也无法与整数连接
- 如何理解句子的生命周期始于对e的评估
- 该程序将.csv文件中的一系列单词放入数组中,然后随机生成句子.但它不起作用
- 我可以擦除 std::queue 中间的节点吗?
- 如何在标准c ++中流式传输/读取二进制文件的中间部分并写入另一个文件?
- 如何在 FOR 语句中间阻止 Visual Studio 2017 c++ 自动完成)?
- 给定类型的模板化中间数组
- 如何在文本文件中间插入字符
- 如何让正则表达式匹配句子中间?