正则表达式检测typedef / class关键字后面的任何内容?

时间:2021-04-19 20:14:54

I've been playing around with GEdit's syntax highlighting. I love the way that Visual Studio highlight's user created types. I would like to do this for my user created types in C/C++ (eg. typedef's/classes). For example (in C):

我一直在玩GEdit的语法高亮。我喜欢Visual Studio突出显示用户创建类型的方式。我想为用户在C / C ++中创建的类型(例如typedef的/ classes)执行此操作。例如(在C中):

typedef struct Node *pNode;

And an example in C++:

以及C ++中的一个例子:

class BigNumber 
{ 
    // Class stuff here.
};

See the way Node is highlighted differntly from typedef struct (keywords) and *pNode isn't highlighted at all. How can I write a regex to detect that, and highlight all occurrences of Node and BigNumber throughout my current document?

请参阅节点突出显示的方式与typedef struct(keywords)不同,* pNode根本不突出显示。如何编写正则表达式来检测它,并在我当前的文档中突出显示所有出现的Node和BigNumber?

1 个解决方案

#1


While Regex's will give you good results, they won't ever give you perfect results.

虽然Regex会给你很好的结果,但他们永远不会给你完美的结果。

Most regex engines do not support the notion of recursion. That is, it cannot match any type of expression which requires counting (matched braces, parens, etc ...). This means it will not be able to match a typedef which points to a function pointer in a reliable fashion.

大多数正则表达式引擎不支持递归的概念。也就是说,它无法匹配任何需要计数的表达式(匹配的括号,parens等......)。这意味着它将无法匹配以可靠方式指向函数指针的typedef。

To get perfect matches you really need to write a parser.

要获得完美匹配,您需要编写解析器。

I think a better approach is to pick the scenarios you care about the most and write regex's which target those specific scenarios.

我认为更好的方法是选择您最关心的场景,并编写针对这些特定场景的正则表达式。

For instance here is a regex which will match typedefs of structs which point to a single name and may or may not have a pointer.

例如,这里是一个正则表达式,它将匹配结构的typedef,它指向一个名称,可能有也可能没有指针。

"^\s*typedef\s+struct\s+\w+\s+((\*?\s*)\w+)\s*;\s*$"

#1


While Regex's will give you good results, they won't ever give you perfect results.

虽然Regex会给你很好的结果,但他们永远不会给你完美的结果。

Most regex engines do not support the notion of recursion. That is, it cannot match any type of expression which requires counting (matched braces, parens, etc ...). This means it will not be able to match a typedef which points to a function pointer in a reliable fashion.

大多数正则表达式引擎不支持递归的概念。也就是说,它无法匹配任何需要计数的表达式(匹配的括号,parens等......)。这意味着它将无法匹配以可靠方式指向函数指针的typedef。

To get perfect matches you really need to write a parser.

要获得完美匹配,您需要编写解析器。

I think a better approach is to pick the scenarios you care about the most and write regex's which target those specific scenarios.

我认为更好的方法是选择您最关心的场景,并编写针对这些特定场景的正则表达式。

For instance here is a regex which will match typedefs of structs which point to a single name and may or may not have a pointer.

例如,这里是一个正则表达式,它将匹配结构的typedef,它指向一个名称,可能有也可能没有指针。

"^\s*typedef\s+struct\s+\w+\s+((\*?\s*)\w+)\s*;\s*$"