Regex只匹配匹配组中许多可能性中的前两个
Regex matches only the first two of many possibilities in a matching group
我注意到,c++11正则表达式只"考虑"了包含4个选项的匹配组中的前两个选项。根据regex101,regex是正确的,如果我更改匹配组中元素的顺序,前两个总是会被识别,但后面的不会被识别。代码非常简单,有没有遗漏什么?
#include <iostream>
#include <regex>
using namespace std;
int main()
{
std::regex staticFilesMatcher(".*\.(ico|jpg|png|gif)", std::regex_constants::ECMAScript | std::regex_constants::icase);
if (std::regex_match("test.ico", staticFilesMatcher))
std::cout << "ICO file recognized" << std::endl;
if (std::regex_match("test.jpg", staticFilesMatcher))
std::cout << "JPG file recognized" << std::endl;
if (std::regex_match("test.png", staticFilesMatcher))
std::cout << "PNG file recognized" << std::endl;
if (std::regex_match("test.gif", staticFilesMatcher))
std::cout << "GIF file recognized" << std::endl;
return 0;
}
奇怪的是,输出是
ICO file recognized
JPG file recognized
Process returned 0 (0x0) execution time : 0.002 s
Press ENTER to continue.
感谢您的帮助,让我找到解决方案。
根据您的评论,您使用的是GCC 4.8.2。但是regex
直到GCC 4.9才得以实施。如果不能升级编译器,就必须使用boost.regex
。
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 比较两个大小不等的映射c++
- C++需要帮助从用户那里获得一个整数,并确保它在另外两个整数之间
- Regex只匹配匹配组中许多可能性中的前两个
- 正在搜索交换两个关联容器的无锁可能性
- 从字符串中删除两个字符以获得唯一字符的可能性