简体   繁体   中英

split in java adds empty space to string array

im trying to split a string using a REGEX. The problem is that when splitting it adds an empty space to my String Array

  public static String[] line;

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

meth.regexMethHead being the regular expresion i want to split with.

the output given is the next one:

Metodos Contenido [0]:

Metodos Contenido [1]: {lol;}

Metodos Contenido [2]: {lol;}}

the output wanted is:

Metodos Contenido [0]: {lol;}

Metodos Contenido [1]: {lol;}}

Regex used

Regex used:

 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;

Input String

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+"";

Keep in mind my regex and String come from other class and im pulling them to make the split in another class

THANKS

我意识到,当您要拆分时,如果要拆分的表达式以REGEX中的某个值开头,则它假定那里有东西(在正则表达式用于拆分之前),因此这就是为什么要保存为空的原因一开始的空间。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM