Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 19-Sep-18 23:50pm

1 solution

Quote:
I have added * in regex but it does not work.

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*


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[^]
 
Share this answer
 
v3
Comments
Pruthvi@123 20-Sep-18 6:06am    
@Patrice T-At Which location i need to add that as i have tried to add it like this(after static \s) like below:
Hide   Copy Code
FINDFUNC_RE = re.search('^\s*(?:(?:inline|static)\s*+\*){0,2}(?!else|typedef|return)\w+\s+\*?\s*(\w+)\s*\([^0]+\s*?', Line)

Though it's not working.And also it should match both:
static int abc(int a)
static int* abc(int a)
Please Reply.Thank You.
Patrice T 20-Sep-18 7:48am    
Use Improve question to update your question.
So that everyone can pay attention to this information.

I have no time now for the regex, start by removing the * you added previously.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900