c - Lex(词法分析器)中正则表达式的大问题

标签 c regex lex lexical-analysis

我有一些这样的内容:

    author = "Marjan Mernik  and Viljem Zumer",
    title = "Implementation of multiple attribute grammar inheritance in the tool LISA",
    year = 1999

    author = "Manfred Broy and Martin Wirsing",
    title = "Generalized
             Heterogeneous Algebras and
             Partial Interpretations",
    year = 1983

    author = "Ikuo Nakata and Masataka Sassa",
    title = "L-Attributed LL(1)-Grammars are
             LR-Attributed",
    journal = "Information Processing Letters"

而且我需要捕获title 双引号之间的所有内容。我的第一次尝试是这样的:

^(""|\t)+"title"""*=""*"\"".+"\","

它捕捉到第一个例子,但没有捕捉到另外两个。另一个有多行,这就是问题所在。我想在某处更改为带有 \n 的内容以允许多行,如下所示:

^(""|\t)+"title"""*=""*"\""(.|\n)+"\","

但这并没有帮助,相反,它捕获了一切

虽然我想,“我想要的是在双引号之间,如果我捕获所有东西直到找到另一个 后跟 , 怎么办?这样我就可以知道我是否在 title 的末尾,无论行数如何,如下所示:

^(""|\t)+"title"""*=""*"\""[^"\""]+","

但这还有另一个问题……上面的例子没有,但是双引号(")可以在title声明之间。例如:

title = "aaaaaaa \"X bbbbbb",

是的,它前面总是有一个反斜杠 (\)。

有任何修复此正则表达式的建议吗?

最佳答案

匹配双引号中字符串的经典正则表达式是:

\"([^\"]|\\.)*\"

在你的情况下,你会想要这样的东西:

"title"\ *=\ *\"([^\"]|\\.)*\"

PS:恕我直言,您在正则表达式中使用了太多引号,很难阅读。

关于c - Lex(词法分析器)中正则表达式的大问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2527470/

相关文章:

c++ - Lex/Yacc 解析器解析列中包含标题和值的文件

python - 将任意长度的列表或元组传递给用 C 编写的 Python 扩展

c - C 编程新手,需要指导

c - 关于 C 中的 strtok 和 token

html - String Replace Regex like mustache js for Ruby

parsing - 用于 R 的 Lex 和 YACC 类工具?

c - 逗号运算符是什么意思?

PHP URL 正则表达式和参数

regex - 如何从 .htaccess 的正则表达式中的 url 捕获 %20?

c++ - 弹性/Bison : Bad token management?