[英]split in java adds empty space to string array
我試圖使用正則表達式拆分字符串。 問題是,拆分時會在我的字符串數組中添加一個空格
public static String[] line;
line=this.classMetodos.split(meth.regexMethHead);
meth.regexMethHead是我要拆分的常規表達式。
給定的輸出是下一個:
Metodos Contenido [0]:
Metodos Contenido [1]:{lol;}
Metodos Contenido [2]:{lol;}}
所需的輸出是:
Metodos Contenido [0]:{lol;}
Metodos Contenido [1]:{lol;}}
正則表達式使用
使用的正則表達式:
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+"";
請記住,我的正則表達式和String來自其他類,並且我將它們拉到另一個類中進行拆分
謝謝
我意識到,當您要拆分時,如果要拆分的表達式以REGEX中的某個值開頭,則它假定那里有東西(在正則表達式用於拆分之前),因此這就是為什么要保存為空的原因一開始的空間。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.