- I have a string which has below values. i need to get function names out of it. and the function names are dynamic.
Using below code i am able to get how many times the word "function" has been occurred. no idea of how to get the function names.
String strLine=request.getParameter("part1"); if(strLine!=null){ String findStr = "function "; int lastIndex = 0; int count =0; while(lastIndex != -1){ lastIndex = strLine.indexOf(findStr,lastIndex); if( lastIndex != -1){ count ++; lastIndex+=findStr.length(); } } System.out.println("count "+count); }part1 is a value from the user. which can be,
function hello(){ } function here(){ }In the above thing, no of functions and function names get changed.
I want to get, hello() and here() as output.
3 Answers
if I have understood your question right, you try to parse the string part1 and you want to get the function names. They are dynamically, thus you cannot make any assumptions about the name. In this case you either have to write your own parser or you use regular expressions.
The following program extracts the function names using a regular expression:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Stackoverflow {
private static Pattern pattern = Pattern.compile("\\s([a-zA-Z0-9_]+\\(\\))", Pattern.DOTALL | Pattern.MULTILINE);
public static void main(String[] args) {
String part1 = "function hello(){\n" +
" }\n" +
" function here(){\n" +
" }";
Matcher matcher = pattern.matcher(part1);
while (matcher.find()) {
String str = matcher.group();
System.out.println(str);
}
}
}
The output is:
hello()
here()
I hope this answers your question.
Comments
@ Bobby rachel. Sorry i didnt understand your question. But if you wAnt to retrieve names you can XML format. and then retrieve from it.
For EXAMPLE String userid=request.getParameter("part1");
String stri = "req=<header><requesttype>LOGIN</requesttype></header>"
+ "<loginId>"
+ userid //the string you get and want to retrieve
+ "</loginId>" //from this whole string
object.searchIn(String loginId) // enter the name of par you want to retrieve
Another function to retrieve value of userid
public String serachIn(String searchNode) { try {
int firstpos = stri.indexOf("<" + searchNode + ">");
int endpos = stri.indexOf("</" + searchNode + ">");
String nodeValue = stri.substring(firstpos + searchNode.length() + 2, endpos);
System.out.println("node value"+searchNode+nodeValue);
return nodeValue;
}
I hope it helps
Comments
You can achieve this using regex, here is an example:
public static List<String> extractFunctionsNames(String input) {
List<String> output = new ArrayList<String>();
Pattern pattern = Pattern.compile("(function\\s+([^\\(\\)]+\\([^\\)]*\\)))+");
Matcher matcher = pattern.matcher(input);
while (matcher.find()) {
output.add(matcher.group(2));
}
return output;
}
public static void main(String[] args) {
String input = "function hello(){\n"
+ " \n}"
+ "\nfunction here(){\n"
+ "}\n";
System.out.println(extractFunctionsNames(input));
}
OUTPUT:
[hello(), here()]
Note that this code isn't reliable because an input like function hello() {print("another function test()")} will output [hello(), test()]
function hello() {print("hi from function hello")}?