有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java中的split为字符串数组添加了空空间

我正在尝试使用正则表达式拆分字符串。问题是,在拆分时,它会在字符串数组中添加一个空格

  public static String[] line;

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

冰毒。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+"";

请记住,我的正则表达式和字符串来自另一个类,我拉它们在另一个类中进行拆分

谢谢


共 (1) 个答案

  1. # 1 楼答案

    我意识到,当你想要分割的时候,如果你想要分割的表达式以正则表达式中的某个值开头,它会判断那里有什么东西(在正则表达式wich将用来分割之前),所以这就是为什么它一开始就节省了一个空的空间