普通的前pressions用括号匹配 [英] Regular expressions with matching brackets

查看:131
本文介绍了普通的前pressions用括号匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提取C源$ C ​​$ C比硬codeD变量。我剩下的问题是,我想解析数组初始化,例如:

I'm trying to extract specific hard coded variables from C source code. My remaining problem is that I'd like to parse array initialisation, for example:

#define SOMEVAR { {T_X, {1, 2}}, {T_Y, {3, 4}} }

这足以解析此例中为{T_X,{1,2}和{T_Y,{3,4}},因为它是那么可能改乘得到完整的结构。然而,它需要足够一般以便能够解析的任何用户定义的类型。

It's enough to parse this example into "{T_X, {1, 2}}" and "{T_Y, {3, 4}}", since it's then possible to recurse to get the full structure. However, it needs to be sufficiently general so as to be able to parse any user defined types.

更妙的是常规的前pressions,可用于从一般的C code构造的额外的价值,比如的#define ,枚举和列表全局变量。

Even better would be a list of regular expressions that can be used to extra values from general C code constructs like #define, enums and global variables.

在C code是提供给我,所以我必须在它的控制。我宁愿不写分析它的字符在时间的函数。然而,这将会是确定定期EX pressions的序列。

The C code is provided to me, so I have no control over it. I'd rather not write a function that parses it a character at a time. However, it'd be OK to have a sequence of regular expressions.

这是没有得到的文件到MATLAB或基本的正前pressions的问题。我一个特定常规的前pression通过支架preserves分组后我。

This is not a problem of getting files into MATLAB or basic regular expressions. I'm after a specific regular expression that preserves groupings by brackets.

编辑:看起来像普通的前pressions不做递归或任意深度匹配。据<一个href=\"http://stackoverflow.com/questions/546433/regular-ex$p$pssion-to-match-outer-brackets\">here和<一个href=\"http://stackoverflow.com/questions/524548/regular-ex$p$pssion-to-detect-semi-colon-terminated-c-for-while-loops\">here.

Looks like regular expressions don't do recursion or arbitrarily deep matches. According to here and here.

推荐答案

编辑:现在这个问题已经被更新,看来我的previous答案忽略了一点。我不知道你是否已经搜查堆栈溢出其他正规-EX pression相关的问题。在你还没有,我在两个来的机会,可以帮助给你指导你的问题(这似乎是一个问题,至少部分地,试图匹配并跟踪打开和关闭大括号):< A HREF =htt​​p://stackoverflow.com/questions/546433/regular-ex$p$pssion-to-match-outer-brackets>这个和<一个href=\"http://stackoverflow.com/questions/524548/regular-ex$p$pssion-to-detect-semi-colon-terminated-c-for-while-loops\">this 之一。祝你好运!

Now that the question has been updated, it appears that my previous answer missed the point. I don't know if you've already searched the other regular-expression-related questions on Stack Overflow. On the chance that you haven't, I came across two that may help give you guidance for your problem (which appears to be a problem, at least partially, of trying to match and keep track of opening and closing curly braces): this one and this one. Good luck!

这篇关于普通的前pressions用括号匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆