如何在Flex中编写以下正则表达式? [英] How to write the following regex in Flex?
问题描述
我试图在flex中定义一个将捕获多行字符串的规则。
多行字符串是以三个撇号开头的字符串:''
以三个撇号结尾,并且可以跨越多行。
例如:
'''这是
一个例子
a multiline
string'''
所以我的尝试是这样的:
%{
#include< iostream> ;
using std :: cout;
using std :: endl;
%}
MULTI_LN_STR'''(。| \\\
)*'''
%%
{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;
}
这适用于输入:
'''这是
a multi
line
string!'''
这是
一些随机
文本
输出为:
GotIt!
这是
一些随机的
文本
但对此输入无效(或更准确,产生错误输出):
'''这是
a multi
line
string!'''
这是
一些随机的
文本
'''and this
is another
multiline
string'''
其产生:
GotIt!这个原因是因为我的规则是:
扫描三个撇号,后跟任何可能的字符,后跟三个撇号,
,而是应该说:
扫描三个撇号,后跟任何可能的字符,除了三个撇号
解决方案对于这样的简单否定,构造正则表达式相对容易:
''([^'] |'[^'] |[^'])*'''
I'm trying to define a rule in flex that will capture a "multiline string".
A multiline string is a string that starts with three apostrophes: '''
, ends with three apostrophes, and can span over multiple lines.
For example:
'''This is
an example of
a multiline
string'''
So my attempt at this was this:
%{
#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;
}
Which works for the input:
'''This is
a multi
line
string!'''
This is
some random
text
The output is:
GotIt!
This is
some random
text
but does not work (or, to be more accurate, produces wrong output) for this input:
'''This is
a multi
line
string!'''
This is
some random
text
'''and this
is another
multiline
string'''
Which produces:
GotIt!
This reason is because my rule says:
"scan for three apostrophes, followed by any possible character, followed by three apostrophes",
but rather, it should say:
"scan for three apostrophes, followed by any possible character except three apostrophes, followed by three apostrophes".
How can I do that?
解决方案 For a simple negation like this, it is relatively easy to construct a regular expression:
"'''"([^']|'[^']|''[^'])*"'''"
这篇关于如何在Flex中编写以下正则表达式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!