我试图使用REGEX分割字符串。问题是,在拆分时,它会为我的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+"";
请记住我的正则表达式和字符串来自其他类并且我将它们拉到另一个类中进行拆分
感谢
答案 0 :(得分:0)
我意识到当你想要分割时,如果你要分割的表达式在REGEX中以某个值开始,那么就会产生那些东西(在正则表达式将用于分割之前),这就是为什么它是在开始时节省空的空间。