2014-12-04 182 views
0

即時嘗試使用REGEX拆分字符串。問題是,分裂時,增加了一個空的空間,我的字符串數組在java中分割爲字符串數組添加空格

public static String[] line; 

    line=this.classMetodos.split(meth.regexMethHead); 

meth.regexMethHead作爲正規表示我想和分裂。

給出的輸出是下一個:

Metodos Contenido [0]:

Metodos Contenido [1]:{洛爾;}

Metodos Contenido [2]:{洛爾;} }

輸出希望是:

Metodos Contenido [0]:{洛爾;}

Metodos Contenido [1]:{笑;}}

正則表達式中使用

正則表達式中使用:

public static String regexAccess = "(public|private|\\s*)"; 

    public static String regexStatic = "(static|\\s*)"; 

    public static String regexReturnType = "(String|double|void)"; 

    public static String regexMethName = "([a-z]|[A-Z])([a-z]|[A-Z]|[0-9])*"; 

    public static String regexVariableName = "([a-z]|[A-Z])([a-z]|[A-Z]|[0-9])*"; 

    public static String regexString = "\"([a-z]|[A-Z]|[0-9])*\""; 

    public static String regexArgs="(\\(\\)|\\(("+regexReturnType+"\\s*"+regexVariableName+")\\))"; 

    public static String regexMethContent="\\{(.*|\\n*|\\t*)*\\}"; 

    //complete regular expression 
    public static String regexMethHead = regexAccess + 
      "\\s*"+ 
      regexStatic+ 
      "\\s*"+ 
      regexReturnType+ 
      "\\s*"+ 
      regexMethName+ 
      "\\s*"+ 
      regexArgs; 

輸入字符串

public static String meth= "public static void jenny(String lolito){\n\t lol;\n\n\t}"; 
    public static String meth2= " public void Lolamer(String jeny){\n\t lol;\n\n\t}"; 
    public static String variables ="static String e =\"lol\";"; 
    public static String pseudoClass="public class torito{"+"\n"+variables+"\n"+ 
      meth+"\n"+meth2+"}"; 
    public static String regexClassName = "([a-z]|[A-Z])([a-z]|[A-Z]|[0-9])*"; 
    public static String regexClassContent="\\{(.*|\\n*|\\t*)*\\}"; 
    public static String regexCLass="^(public|private|\\s*)\\s*class\\s+"+ 
      regexClassName+""+ 
      regexClassContent+""; 

記住我的正則表達式和字符串來自其他職業和IM拉他們在另一個類別的分裂

謝謝

+2

提供使用的輸入字符串和正則表達式。 – anubhava 2014-12-04 21:00:23

+0

剛剛編輯與輸入字符串和正則表達式使用的帖子!幫幫我!! – Hbassuki 2014-12-04 21:06:08

+0

讓我把你介紹給我的朋友,[ANTLR](http://www.antlr.org/)。 – 2014-12-04 21:27:53

回答

0

我意識到,當你想分割,如果你想分割的表達式開始於REGEX中的某個值,它假設那裏有東西(在正則表達式將用於分割之前),這樣爲什麼它在開始時節省了一個空的空間。