I am working on making a regex to recognize a function for my new programming language's syntax highlighting file for vim. I cannot for the life of me understand how to make it match this though.
it would need to match all functions in an example like this:
int addFive() {
}
addFive()
int addFive()
{
}
int value = addFive ()
addFive(int number)
void sum ( string hello )
etc...
but cannot figure it out.
here is what I managed to figure out: [a-zA-Z]*()$
CodePudding user response:
I don't know the all of the syntax you're trying to match, but something like this might work:
[a-zA-Z] ?\s*?\(.*?\)
[a-zA-Z] ?
matches alphabet characters, if you want numbers as well you could do [a-zA-Z0-9] ?
\s*?
matches possible whitespace between the function name and the parentheses
\(
matches the first parenthesis
.*?
matches any characters inside the parentheses
\)
matches the last parenthesis
test it out: https://regex101.com/r/L2guif/1
CodePudding user response:
It is possible to use grep
to identify text across lines with -z
option with -o
option.
grep -z -o ")[[:space:]]\ {[^}]*}"
But that is a bad idea.
If the function may contain brackets.
For example:
int funct1 () {
int 1;
for (i = 0; i < 3; i ) {
print i;
}
}
The RegExp will match the closing bracket, in internal for-loop
bracket.
RegExp cannot include logic.
RegExp difficult to distinguish for-loop
or if-condition
from a function.
You probably need lexical analyzer like lex
to identify the structure of your program.