Regex 如何在lex中识别扫描仪的字符串文字?

Regex 如何在lex中识别扫描仪的字符串文字?,regex,lex,Regex,Lex,好的,我一直在尝试在lex中找到一个正则表达式,它可以识别输入字符串中类似C的字符串文字。在printfstackoverflow中,stackoverflow应该被识别为字符串文字。 我一直在尝试以下方法: "[.]+" ["][.]+["] \"[.]+\" [\"][.]+[\"] """[.]+""" 这些都不起作用。每次识别的词素都是单独的。我该怎么办? 提前感谢…您可能会发现这些链接很有用 您可能会发现这些链接很有用 简单地说,试试这个: \".*\" printf("ST

好的,我一直在尝试在lex中找到一个正则表达式,它可以识别输入字符串中类似C的字符串文字。在printfstackoverflow中,stackoverflow应该被识别为字符串文字。 我一直在尝试以下方法:

"[.]+"
["][.]+["]
\"[.]+\"
[\"][.]+[\"]
"""[.]+"""
这些都不起作用。每次识别的词素都是单独的。我该怎么办?
提前感谢…

您可能会发现这些链接很有用


您可能会发现这些链接很有用

简单地说,试试这个:

\".*\"     printf("STRING[%s]", yytext);
\'.*\'     printf("STRING[%s]", yytext);
编译并运行时,快速测试显示它正确解析字符串,如

"hello world!"
STRING["hello world!"]
'hello world!'
STRING['hello world!']
'john\'s cat'
STRING['john\'s cat']
'mary said "hello!"'
STRING['mary said "hello!"']
"frank said \"goodbye!\""
these are words "which contain" a string
these are words STRING["which contain"] a string
简单地说,试试这个:

\".*\"     printf("STRING[%s]", yytext);
\'.*\'     printf("STRING[%s]", yytext);
编译并运行时,快速测试显示它正确解析字符串,如

"hello world!"
STRING["hello world!"]
'hello world!'
STRING['hello world!']
'john\'s cat'
STRING['john\'s cat']
'mary said "hello!"'
STRING['mary said "hello!"']
"frank said \"goodbye!\""
these are words "which contain" a string
these are words STRING["which contain"] a string
可能的重复可能的重复