遇到问题拆分特定字符串正确

问题描述:

你好,这是我第一次在这里发帖,我环顾四周,我无法找到一个答案,我有这个问题。遇到问题拆分特定字符串正确

我需要把这个字符串转换为两个字符串:

"R(rndInt[-10,10]),R(rndInt[-10,10])" 

"R(rndInt[-10,10])" "R(rndInt[-10,10])" 

为了使长话短说,我正在改装游戏,创造我自己的脚本语言,为人们使用。余由基于类/方法结构(注意事项是,这里:http://pastie.org/8825346)。现在,我要把它让你可以使用类方法中/法(工作单参数的方法)。

我现在遇到的问题是拆分该字符串,因为它有多个逗号。我无法真正想到一种不会造成问题的方法。

任何人都可以帮忙吗?我现在很难过。

编辑:解决!

向前断言方法的工作原理!直到现在我还没有听说过它。

我忘了提及我不能使用“R”。 R引用一个类,它可以有不同的名称。

我想出了通过找出什么是之间对这个问题的解决方案“),”和“(”由子串的字符串。

String cName=oldStr.substring(oldStr.indexOf("),")+2); 
cName=cName.substring(0, oldStr.indexOf("(")); 
System.out.println("CNAME:"+cName); 
String[] test = oldStr.split(",(?="+cName+")"); 

谢谢你们,我非常感激为帮助:)

+0

请不要把[求解]或任何它的变体在你的问题的标题。您在此处标记问题的方式是单击正确答案旁边的复选标记。 –

+0

请不要在标题中加入标签。有一个标签系统已经为此提供。 –

+0

请不要将“编辑”或其任何变体(如ETA)放在问题的正文中。每个问题都有详细的编辑历史,每个人都可以查看;您的问题的编辑历史可以在这里找到:http://*.com/posts/22136224/revisions –

为什么不针对有关逗号:

str.split("\\),R"); // as a simple Regexp 

UPDATE ----

正如评论指出,并通过@Reimeus回答,正则表达式最终会到:

str.split(",(?=R)"); 
+0

您的解决方案必须使分割结果为R(rndInt [-10,10]'和'(rndInt [-10,10])',这不是最佳解决方案。 –

+0

@AdrianShum确切的,因此去寻找前瞻assertion :) – Mik378

你可以使用一个lookahead assertion

String[] array = str.split(",(?=R)"); 
+0

根据OP链接到的语言规范,第二个“R”字符有时可能是“M”。如果我理解正确,最终可能是任何类名。 – aroth

+0

同意,最终如果OP想要鲁棒的解决方案JFlex/ANTLR会走的路 - 这种解决方案只适用于一个快速的解决方案,其中可以使用一组有限的类 – Reimeus

如果你创建你自己的语言,它可能最好把它当作一门语言,并做适当的lex/parse。有一些开源工具可用来协助这个问题,如JFlex

但是,根据您当前的语言要求,您的结构非常简单,您可以按照其他答案中的建议跳脱split()。随着您的语言随着时间的推移,这可能会也可能不会保持可行。

那里有很多脚本语言,你确定重新发明*是最好的利用你的时间吗?如果您是:

对于非平凡语言,解析通常使用parser generator完成。

但是,对于简单的情况,手写它并不难。合理的方法是recursive decent parsing。在你的情况下,这看起来像这样:

import java.util.ArrayList; 
import java.util.List; 

abstract class Expression { 

} 

class MethodInvocation extends Expression { 
    final String methodName; 
    final List<Expression> arguments; 

    MethodInvocation(String methodName, List<Expression> arguments) { 
     this.methodName = methodName; 
     this.arguments = arguments; 
    } 
} 

class Literal extends Expression { 
    final int value; 

    Literal(int value) { 
     this.value = value; 
    } 
} 


public class Parser { 
    char[] input; 

    /** index of next character to consume */ 
    int index; 

    Parser(String input) { 
     this.input = input.toCharArray(); 
     index = 0; 
    } 

    void consumeExpected(char ch) { 
     if (input[index] != ch) { 
      throw new RuntimeException("Syntax Error"); 
     } 
     index++; 
    } 

    String identifier() { 
     int start = index; 
     while (index < input.length && Character.isAlphabetic(input[index])) { 
      index++; 
     } 
     return new String(input, start, index - start); 
    } 

    Literal numericLiteral() { 
     int start = index; 
     if (input[index] == '-') { 
      index++; 
     } 
     while (index < input.length && '0' <= input[index] && input[index] <= '9') { 
      index++; 
     } 
     return new Literal(Integer.parseInt(new String(input, start, index - start))); 
    } 

    Expression expression() { 
     if (Character.isAlphabetic(input[index])) { 
      String methodName = identifier(); 
      consumeExpected('('); 

      List<Expression> arguments = new ArrayList<>(); 
      if (input[index] != ')') { 
       do { 
        arguments.add(expression()); 
       } while (input[index++] == ','); 
       index--; 
       consumeExpected(')'); 
      } 
      return new MethodInvocation(methodName, arguments); 
     } else { 
      return numericLiteral(); 
     } 
    } 

    public static void main(String[] args) { 
     Expression ex = new Parser("R(rndInt(-10,10)),R(rndInt(-10,10))").expression(); 
     System.out.println(ex); 
    } 
}