0
  • 我有一个具有以下值的字符串。我需要从中获取函数名称。并且函数名称是动态的。
  • 使用下面的代码,我可以获得“函数”这个词出现了多少次。不知道如何获取函数名称。

       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 是来自用户的值。这可以是,

           function hello(){
           }
           function here(){
           }
    
  • 在上面的事情中,没有函数和函数名被改变。

  • 我想得到 hello() 和 here() 作为输出。

4

3 回答 3

2

如果我正确理解了您的问题,您尝试解析字符串 part1 并且您想要获取函数名称。它们是动态的,因此您不能对名称做出任何假设。在这种情况下,您要么必须编写自己的解析器,要么使用正则表达式。

以下程序使用正则表达式提取函数名称:

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);
        }
    }
}

输出是:

hello()
here()

我希望这回答了你的问题。

于 2013-10-03T09:17:59.923 回答
0

您可以使用 实现此目的regex,这是一个示例:

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));
}

输出:

[hello(), here()]

请注意,此代码不可靠,因为类似的输入function hello() {print("another function test()")}会输出[hello(), test()]

于 2013-10-03T09:29:49.393 回答
0

@鲍比雷切尔。对不起,我不明白你的问题。但是,如果您想检索名称,您可以采用 XML 格式。然后从中检索。

例如 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) // 输入要检索的 par 名称

检索用户标识值的另一个函数

公共字符串serachIn(字符串搜索节点){尝试{

        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;

    }

我希望它有帮助

于 2013-10-03T09:08:58.593 回答