moor
moor

Reputation: 21

regex find method calls with specific number of arguments

I'm trying to find all occurencas of certain method, but only with specified number of arguments(5):

so let's say I have different methods with the same name and different set of args.

.method(asd,asd,asd,asd,asd,asd,asd)
.method(asd,asd,asd,asd,asd)
.method(asd,asd,asd)

I've tried something like that: \.open\((?:.*?\,){4}[^,]*?\) but it gives back all methods with 5 and more args:

.method(asd,asd,asd,asd,asd,asd,asd)
.method(asd,asd,asd,asd,asd)

I need only those with 4. Thanks in advance!

Upvotes: 2

Views: 1827

Answers (2)

Bart Kiers
Bart Kiers

Reputation: 170268

Try something like this:

\.method\(\w+(,\w+){3}\)

only returns the ones with exactly 4 params. You may want to account for optional space-chars:

\.method\s*\(\s*\w+(\s*,\s*\w+\s*){3}\)

EDIT

Since you've tagged your question with Eclipse, I assume you're familiar with Java. The following:

import java.util.regex.*;

class Test {
  public static void main(String[] args) {
    String text = ".method(asd,asd,asd,asd,asd,asd,asd) \n" +
        ".method(asd,asd,asd,asd)                       \n" +
        ".method(asd,asd,asd)                           \n" +
        "Foo.method(a,b,c,d)                            \n";

    Matcher m = Pattern.compile("\\.method\\(\\w+(,\\w+){3}\\)").matcher(text);

    while(m.find()) {
      System.out.println(m.group());
    }
  }
}

produces the output:

.method(asd,asd,asd,asd)
.method(a,b,c,d)

as you can see on Ideone: http://ideone.com/RvTxw

HTH

Upvotes: 0

user unknown
user unknown

Reputation: 36260

works for me:

egrep "\(([^,]*,){4}[^,]*\)"  method

Suggestion from comment:

egrep "open\s?\(([^,)]*,){4}[^,)]*\)"  methodfile

Upvotes: 2

Related Questions