问题
I need to parse a txt file, from disk. So i have firstly made an example, to test regex.
This is my example code:
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;
So i'm trying to parse until notthis
, but returned match m is a failed match. If i do the regex like this: std::regex rt("paragraph:([\\S\\s]+)");
it works fine, but i get the whole text:
this is the text file
i need only this data
notthis
I haven't used many regex before, but i have been told that c++ using ecmascript syntax, but in the documentation, it seems that the pattern for lookahead is the same, and only lookbehinds are not supported. How can i make a lookahead in the ecmascript?
回答1:
Use as follow:
#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;
}
来源:https://stackoverflow.com/questions/52797969/c-stdregex-using-lookahead-fails