为什么C 正则只能找到JavaScript Regex在字符串中找到的一部分
why does C++ regex only find a part of what javascript regex find in a string?
我需要在我的代码中使用Regex,但它不应正常工作。我想在每行读取的文件中搜索Test.list.everything
,但它仅获得everything
。我缩短了代码以使我的问题清楚。在不同的正则测试程序中,我的表达式正常工作,因为您在此测试程序中可以看到它:http://regex101.com/r/ir1ch1。
我使用的输入是
summer1969 2014-03-07 Test.list.everything.1234 123.4.4.34 moreCode
和Regex (([a-zA-Z]+.{0,1})+).[0-9]+
。
如果我使用std::tr1::regex
而不是std::regex
,它也无法正常工作。我不知道这两个表达式之间的区别,有什么吗?
C 和我使用的其他程序有什么区别?我如何告诉C 正则表达方式像其他方面一样?
#include <regex>
#include <iostream>
int main()
{
std::string output = "summer1969 2014-03-07 Test.list.everything.1234 123.4.4.34 moreCode";
std::cmatch result;
std::regex rx ("(([a-zA-Z]+\.{0,1})+)\.[0-9]+");
std::regex_search(output.c_str(), result, rx);
for(int i = 0; i < result.size(); i++)
{
std::cout << i << ". " << result[i] << std::endl;
}
system("pause");
return(0);
}
输出:
0. everything.1234
1. everything
2. everything.
它看起来像是Visual C 2010中的一个错误。如果要捕获文本,则可以使用点和数字,那么您可以使用以下正格版:
"[a-zA-Z\.]+(?=\.\d+)"
它可以正常工作。
相关文章:
- C++:如何使函数只返回作为列表一部分的字符串
- 如何将字符串的一部分转换为整数
- 如何有效地将一个大std::字符串的一部分转换为float
- 需要从字符串数组的一部分访问元素
- 当 NUL 字符被定义为字符串的一部分时,为什么 strlen() 不计算终止 NUL 字符的字节?
- 如何正确实现 std::all_of 函数来验证字符串的一部分?
- boost::async_write 跳过字符串的一部分
- 如何在不复制的情况下比较字符串的一部分?
- 我如何在QT中获得字符串的一部分
- 如何在C++中比较数组中字符串的一部分的字符串
- 如何在 c++ 中拆分字符串的一部分
- 字符数组仅占用字符串的一部分
- 如何用双C 替换字符串的一部分
- 如何在字符串中选择句子的一部分 (C++)
- 如何拆分 QString 并将一部分字符串保存在浮点数的向量中
- 为什么C 正则只能找到JavaScript Regex在字符串中找到的一部分
- 字符串操纵和忽略字符串的一部分
- 在C 中更新字符串的一部分
- 有条件地确定字符串是否是数组的一部分
- 尝试在 Cocos2d-X C++中设置一个带有整数作为其字符串一部分的 CCLabelTTF