我想为多行样本制作一个正则表达式。我试过这样的:
^"SAMPLE_SIGN"."\n".SAMPLE_SIGN\n std::cout << "MULTIPLE ROW SAMPLE"
但这不适合我。
一个可能的输入:
some program code SAMPLE_SIGN text inside the
sample SAMPLE_SIGN
这个版本的正确版本是什么?
如果你想允许它在行的任何位置,不仅开始然后不应该使用^
并允许你的标志:SAMPLE_SIGN
或:|
行尾:\n
,之后可以是任何东西*
"SAMPLE_SIGN"([^SAMPLE_SIGN]|\n)*"SAMPLE_SIGN" std::cout << "Block"
这将允许您使用SAMPLE_SIGN
作为SAMPLE_SIGN
块中的第一个字符。例如,作为原始评论部分。
试试Regex:SAMPLE_SIGN([\S\s]+)(?=SAMPLE_SIGN)
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;
}