如何在lex中捕获多行

问题描述 投票:1回答:2

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

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

但这不适合我。

一个可能的输入:

some program code SAMPLE_SIGN text inside the 
sample SAMPLE_SIGN

这个版本的正确版本是什么?

c++ regex lex
2个回答
0
投票

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

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

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


-1
投票

试试Regex: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;
}

C++ Code Reference

© www.soinside.com 2019 - 2024. All rights reserved.