如何在Flex中编写以下正则表达式
How to write the following regex in Flex?
我正试图在flex中定义一个捕获"多行字符串"的规则
多行字符串是一个以三个撇号'''
开头、以三个省略号结尾并且可以跨越多行的字符串
例如:
'''This is
an example of
a multiline
string'''
所以我的尝试是:
%{
#include<iostream>
using std::cout;
using std::endl;
%}
MULTI_LN_STR '''(.|n)*'''
%%
{MULTI_LN_STR} {cout<<"GotIt!";}
%%
int main(int argc, char* argv[]) {
yyin=fopen("test.txt", "r");
if (!yyin) {
cout<<"yyin is NULL"<<endl;
return 1;
}
yylex();
return 0;
}
适用于输入的:
'''This is
a multi
line
string!'''
This is
some random
text
输出为:
GotIt!
This is
some random
text
但对于该输入不起作用(或者,更准确地说,产生错误的输出):
'''This is
a multi
line
string!'''
This is
some random
text
'''and this
is another
multiline
string'''
哪个生产:
GotIt!
这是因为我的规则说:
"扫描三个撇号,然后是任何可能的字符,然后是三个撇子",
相反,它应该说:
"扫描三个撇号,然后是任何可能的字符,除了三个撇子,然后是三个撇符"。
我该怎么做?
对于这样一个简单的否定,构造一个正则表达式相对容易:
"'''"([^']|'[^']|''[^'])*"'''"
相关文章:
- 使用正则表达式regex_search在字符串中查找字符串
- 在 C++ 中使用正则表达式错误时出现问题 括号表达式中的范围无效
- C++正则表达式无限循环
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- 定义有趣的宏和正则表达式在Z3 C++绑定
- 带有多个字符分隔符的正则表达式
- C++ 使用增强正则表达式库时断言崩溃
- 有人可以帮助我处理正则表达式吗?
- 使用正则表达式获取大括号块的列表
- 正则表达式以匹配数字的重复模式,后跟任何类型的分隔符?
- 组合正则表达式部分使用 | 不起作用的 C++
- 为什么C++正则表达式这么慢?
- 如何使HTML5电子邮件验证正则表达式在C++中工作?
- 在C++中实现正则表达式
- C++正则表达式替换整个单词
- 用C++编写正则表达式的正确方法是什么?
- 如何从Stroustrup的C++书中解释这个正则表达式?
- c++正则表达式/flex++中的模式{sp}是什么意思
- 如何在Flex中编写以下正则表达式
- flex / c ++正则表达式逻辑错误