Gideon Isaac
Gideon Isaac

Reputation: 405

How would I use a regular expression to find calls to functions?

Suppose I want find all function calls in my listing (a vb.net listing), and I have the function name. first I thought I could do a regular expression such as:

myfunc\(  .*  \)

That should work even if the function spans multiple lines, assuming that the dot is interpreted as including newlines (there is an option to do this in dot-net)

but then I realized that some of my arguments themselves could be function calls. in other words:
myfunc( a,b,c,d(),e ), which means that the parentheses don't match up.

so I thought that since the main function call usually is the first item on a line, I could do this:

^myfunc( .* \) $

The idea is that the function is the first item on a line (^) and the last paren is the last item on a line ($). but that doesn't work either.

What am I doing wrong?

Upvotes: 0

Views: 116

Answers (2)

chue x
chue x

Reputation: 18803

This is not a direct answer to your question, but if you want to find all uses of your function you can use Visual Studio. Just right click on the function, and then select Find All References:

enter image description here

Visual Studio will show you the results. You can then double click on each line and Visual Studio will take you there.

enter image description here

Upvotes: 0

mrks
mrks

Reputation: 8333

You can't. By design, regular expressions cannot deal with recursion which is needed here.

For more information, you might want to read the first answer here: Can regular expressions be used to match nested patterns?

And yes, I know that some special "regular expressions" do allow for recursion. However, in most cases, this means that you are doing something horrible. It is much better to use something that can actually understand the syntax of your language.

Upvotes: 3

Related Questions