如何在 lex 中捕获多行
How to catch multiple rows in lex
本文关键字:lex 更新时间:2023-10-16
我想为多行示例制作一个正则表达式。 我试过这样:
^"SAMPLE_SIGN"."n".SAMPLE_SIGNn 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
块中的第一个字符。例如,作为原始注释部分。
尝试正则表达式:SAMPLE_SIGN([Ss]+)(?=SAMPLE_SIGN)
演示
C++代码演示:
#include <iostream>
#include <string>
#include <regex>
int main()
{
std::string txt("some program code SAMPLE_SIGN text inside thernsample 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++代码参考
相关文章:
- 没有找到相关文章