我需要从磁盘解析 txt 文件。所以我首先做了一个例子来测试正则表达式。
这是我的示例代码:
std::string txt("paragraph:\r\nthis is the text file\r\ni need only this data\r\nnotthis");
std::smatch m;
std::regex rt("paragraph:([\\S\\s](?=notthis))");
std::regex_search(txt, m, rt);
std::cout << m.str(1) << std::endl;
所以我试图解析直到notthis
,但返回的匹配 m 是失败的匹配。如果我像这样执行正则表达式:std::regex rt("paragraph:([\\S\\s]+)");
它工作正常,但我得到了全文:
this is the text file
i need only this data
notthis
我之前没有使用过很多正则表达式,但有人告诉我c++使用ecmascript语法,但在文档中,lookahead的模式似乎是相同的,并且仅不支持lookbehinds。我如何在 ecmascript 中进行前瞻?
使用方法如下:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string txt("paragraph:\r\nthis is the text file\r\ni need only this data\r\nnotthis");
std::smatch m;
std::regex rt("paragraph:([\\S\\s]+(?=notthis))");
std::regex_search(txt, m, rt);
std::cout << m.str(1) << std::endl;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)