Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java将方法字符串拆分为方法名称和参数_Java_Arrays_Regex_String - Fatal编程技术网

Java将方法字符串拆分为方法名称和参数

Java将方法字符串拆分为方法名称和参数,java,arrays,regex,string,Java,Arrays,Regex,String,我正在为我正在制作的游戏编写一种小型编程语言,这种语言将允许用户在内部游戏代码之外为向导实体定义自己的咒语。我已经写下了语言,但我不完全确定如何更改字符串,如 setSpellName("Fireball") setSplashDamage(32,5) 放入一个数组中,该数组后面有方法名和参数,如 {"setSpellName","Fireball"} {"setSplashDamage","32","5"} 如何使用java的String.split或String regex实现这一点?

我正在为我正在制作的游戏编写一种小型编程语言,这种语言将允许用户在内部游戏代码之外为向导实体定义自己的咒语。我已经写下了语言,但我不完全确定如何更改字符串,如

setSpellName("Fireball")
setSplashDamage(32,5)
放入一个数组中,该数组后面有方法名和参数,如

{"setSpellName","Fireball"}
{"setSplashDamage","32","5"}
如何使用java的String.split或String regex实现这一点? 提前感谢。

捕获字符串

setSpellName("Fireball")
这样做:

String[] line = argument.split("(");
获取第[0]行的“SetSpillName”和第[1]行的“Fireball”)

去掉像这样的最后一个括号

line[1].replaceAll(")", " ").trim();
用两个“清理过的”字符串构建JSON


使用正则表达式可能有更好的方法,但这是一种快速而肮脏的方法。

因为您只对函数名和参数感兴趣,我建议您扫描到参数的第一个实例(然后扫描到最后一个实例)

String input = "setSpellName(\"Fireball\")";
String functionName = input.substring(0, input.indexOf('('));
String[] params = input.substring(input.indexOf(')'), input.length - 1).split(",");
使用
String.indexOf()
String.substring()
,可以解析出函数和参数。一旦你把它们解析出来,在它们周围加上引号。然后将它们重新组合在一起,以逗号分隔,并用大括号括起来

public static void main(String[] args) throws Exception {
    List<String> commands = new ArrayList() {{
        add("setSpellName(\"Fireball\")");
        add("setSplashDamage(32,5)");
    }};

    for (String command : commands) {
        int openParen = command.indexOf("(");
        String function = String.format("\"%s\"", command.substring(0, openParen));
        String[] parameters = command.substring(openParen + 1, command.indexOf(")")).split(",");

        for (int i = 0; i < parameters.length; i++) {
            // Surround parameter with double quotes
            if (!parameters[i].startsWith("\"")) {
                parameters[i] = String.format("\"%s\"", parameters[i]);
            }
        }

        String combine = String.format("{%s,%s}", function, String.join(",", parameters));
        System.out.println(combine);
    }
}

这是一个使用正则表达式的解决方案,请使用此正则表达式
“([\\w]+)\(\”?([\\w]+)\“?\”

String input=“setSpellName(\“Fireball\”);
字符串模式=“([\\w]+)\(\”?([\\w]+)\“?\”;
Pattern r=Pattern.compile(Pattern);
字符串[]匹配;
匹配器m=r.匹配器(输入);
if(m.find()){
System.out.println(“找到值:+m.group(1));
System.out.println(“发现值:+m.group(2));
字符串[]params=m.group(2).split(“,”);
如果(参数长度>1){
matches=新字符串[params.length+1];
匹配[0]=m.group(1);
系统输出打印项次(参数长度);
对于(int i=0;i
([\\w]+)
是第一个获取函数名的组

\(\”?([\\w]+)\“?\)
是获取参数的第二组


这是

您能将一个方法传递给一个方法吗?如果是的话,你需要一个解析器。什么是允许的字符?像
“Foo Bar Baz”
这样的拼写名称中是否有空格?像
“Fireball(small)”这样的名称中是否有括号?如果是的话,你对他们的期望是什么?这太完美了!谢谢您的回复。@johnic431首先,这不可编译!!!其次,“String[]params=input.substring(input.indexOf('))”、input.length-1.split(“,”);“如何获取参数?可能需要一些调整,我昨晚就这么做了。它的工作原理如下:1)对于函数名,我们知道函数名不能包含大括号,因此这将使字符串达到第一个开括号的值,即函数名。2) 同样的情况也适用于参数,我们从括号内开始,在结尾处剪切字符串,-1以说明结尾处的括号。然后用逗号分割里面的所有内容以获得参数。
{"setSpellName","Fireball"}
{"setSplashDamage","32","5"}
    String input = "setSpellName(\"Fireball\")";
    String pattern = "([\\w]+)\\(\"?([\\w]+)\"?\\)";

    Pattern r = Pattern.compile(pattern);
    String[] matches;
    Matcher m = r.matcher(input);
    if (m.find()) {
        System.out.println("Found value: " + m.group(1));
        System.out.println("Found value: " + m.group(2));

        String[] params = m.group(2).split(",");
        if (params.length > 1) {
            matches = new String[params.length + 1];
            matches[0] = m.group(1);
            System.out.println(params.length);
            for (int i = 0; i < params.length; i++) {
                matches[i + 1] = params[i];
            }
            System.out.println(String.join(" :: ", matches));
        } else {
            matches = new String[2];
            matches[0] = m.group(1);
            matches[1] = m.group(2);
            System.out.println(String.join(", ", matches));
        }
    }