正则表达式以满足以下要求 [英] Regular expression for following requirement.
问题描述
我正在使用以下正则表达式来匹配函数。
I am using following regular expression for matching function.
FINDFUNC_RE = re.search('^\s*(?:(?:inline|static)\s+){0,2}(?!else|typedef|return)\w+\s+\*?\s*(\w+)\s*\([^0]+\s*?', Line)
如果行是这样的话,它会正确匹配:
It correctly matches if line is like this:
static int abc(int a)
但是如果该行是像这样(返回类型后包含*):
But it does not matches if the line is like this(Contain * after return type):
static int* abc(int a)
如何正确匹配两个字符串?
我尝试过:
How to match both string correctly?
What I have tried:
<pre>FINDFUNC_RE = re.search('^\s*(?:(?:inline|static)\s*+){0,2}(?!else|typedef|return)\w+\s+\*?\s*(\w+)\s*\([^0]+\s*?', Line)
我在正则表达式中添加*但它不起作用。
I have added * in regex but it does not work.
推荐答案
我在正则表达式中添加*但它不起作用。
I have added * in regex but it does not work.
按照RegEx定义, * 是RegEx中的一个特殊字符,为了匹配它必须将其转义为 \ *
我得到它,原始RegEx,空间必须是可选的:
As per RegEx definition, * is a special char in a RegEx, to match it one must escape it as \*
I got it, from original RegEx, the space must be optional:
^\s*(?:(?:inline|static)\s+){0,2}(?!else|typedef|return)\w+\s+\*?\s*(\w+)\s*\([^0]+\s*?
^^^
\s*
只是一些有趣的链接帮助构建和调试RegEx。
以下是RegEx文档的链接:
perlre - perldoc.perl.org [ ^ ]
以下是帮助构建RegEx并调试它们的工具的链接:
.NET Regex Tester - Regex Storm [ ^ ]
Expresso正则表达式工具 [ ^ ]
RegExr:Learn,Build,&测试RegEx [ ^ ]
在线正则表达式测试器和调试器:PHP,PCRE,Python,Golang和JavaScript [ ^ ]
这个显示RegEx是一个很好的图表,它非常有助于理解RegEx的作用:
Debuggex:在线可视正则表达式测试器。 JavaScript,Python和PCRE。 [ ^ ]
这个网站也在一个漂亮的图表中显示正则表达式,但无法测试与RegEx匹配的内容:
Regexper [ ^ ]
Just a few interesting links to help building and debugging RegEx.
Here is a link to RegEx documentation:
perlre - perldoc.perl.org[^]
Here is links to tools to help build RegEx and debug them:
.NET Regex Tester - Regex Storm[^]
Expresso Regular Expression Tool[^]
RegExr: Learn, Build, & Test RegEx[^]
Online regex tester and debugger: PHP, PCRE, Python, Golang and JavaScript[^]
This one show you the RegEx as a nice graph which is really helpful to understand what is doing a RegEx:
Debuggex: Online visual regex tester. JavaScript, Python, and PCRE.[^]
This site also show the Regex in a nice graph but can't test what match the RegEx:
Regexper[^]
这篇关于正则表达式以满足以下要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!