如何在 lex 中捕获多行

How to catch multiple rows in lex

我想为多行示例制作一个正则表达式。 我试过这样:

^"SAMPLE_SIGN"."\n".SAMPLE_SIGN\n    std::cout << "MULTIPLE ROW SAMPLE"

但这对我不起作用。

可能的输入:

some program code SAMPLE_SIGN text inside the 
sample SAMPLE_SIGN

这个的正确版本是什么?

试试正则表达式:SAMPLE_SIGN([\S\s]+)(?=SAMPLE_SIGN)

Demo

C++ 代码Demo:

#include <iostream>
#include <string>
#include <regex>

int main()
{
std::string txt("some program code SAMPLE_SIGN text inside the\r\nsample SAMPLE_SIGN");
std::smatch m;
std::regex rt("SAMPLE_SIGN([\S\s]+)(?=SAMPLE_SIGN)");
std::regex_search(txt, m, rt);

std::cout << m.str(1) << std::endl;
}

如果你想允许它在行的任何位置,不仅是开始然后不应该使用 ^ 并允许你的标志:SAMPLE_SIGN 或:| 结束行:\n 之后可以是任何内容 *

"SAMPLE_SIGN"([^SAMPLE_SIGN]|\n)*"SAMPLE_SIGN"  std::cout << "Block"

这将允许您使用 SAMPLE_SIGN 作为 SAMPLE_SIGN 和块中的第一个字符。例如作为原始评论部分。