2014-03-03 42 views
2

你好,這是我第一次在這裏發帖,我環顧四周,我無法找到一個答案,我有這個問題。遇到問題拆分特定字符串正確

我需要把這個字符串轉換爲兩個字符串:

"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://stackoverflow.com/posts/22136224/revisions –

回答

1

爲什麼不針對有關逗號:

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

UPDATE ----

正如評論指出,並通過@Reimeus回答,正則表達式最終會到:

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

您的解決方案必須使分割結果爲R(rndInt [-10,10]'和'(rndInt [-10,10])',這不是最佳解決方案。 –

+0

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

1

你可以使用一個lookahead assertion

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

根據OP鏈接到的語言規範,第二個「R」字符有時可能是「M」。如果我理解正確,最終可能是任何類名。 – aroth

+0

同意,最終如果OP想要魯棒的解決方案JFlex/ANTLR會走的路 - 這種解決方案只適用於一個快速的解決方案,其中可以使用一組有限的類 – Reimeus

1

如果你創建你自己的語言,它可能最好把它當作一門語言,並做適當的lex/parse。有一些開源工具可用來協助這個問題,如JFlex

但是,根據您當前的語言要求,您的結構非常簡單,您可以按照其他答案中的建議跳脫split()。隨着您的語言隨着時間的推移,這可能會也可能不會保持可行。

0

那裏有很多腳本語言,你確定重新發明輪子是最好的利用你的時間嗎?如果您是:

對於非平凡語言,解析通常使用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); 
    } 
}