Regex find calls with a certain number of arguments

I try to find everything that happens in a particular method, but only with the specified number of arguments (5):

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

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

I tried something like this: \.open\((?:.*?\,){4}[^,]*?\) , But it returns all methods with 5 or more arguments:

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

I only need those who have 4. Thanks in advance!

+1
source share
2 answers

works for me:

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

Suggestion from the comment:

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

Try something like this:

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

returns only those that have exactly 4 parameters. You can specify additional space characters:

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

EDIT

Since you tagged your question with Eclipse, I assume you are familiar with Java. 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()); } } } 

outputs the result:

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

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

NTN

0
source

Source: https://habr.com/ru/post/909133/


All Articles