Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/369.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用正则表达式拆分字符串_Java_Regex_String - Fatal编程技术网

如何在java中使用正则表达式拆分字符串

如何在java中使用正则表达式拆分字符串,java,regex,string,Java,Regex,String,我有一些字符串模式。每个图案由两个字符“A”和“B”组成。 我的模式像“AA”或“ABA”或“AAABA”或 我想拆分这些模式,这些示例的输出必须是:{“AA”}或{“A”、“B”、“A”}或{“AA”、“BB”、“A”、“B”、“A”} 到目前为止,我尝试的是: String pattern = "AABBABA" //or whatever String firstChar = pattern.toString().substring(1, 2); String[] split = p

我有一些字符串模式。每个图案由两个字符“A”和“B”组成。 我的模式像“AA”或“ABA”或“AAABA”或

我想拆分这些模式,这些示例的输出必须是:{“AA”}或{“A”、“B”、“A”}或{“AA”、“BB”、“A”、“B”、“A”}

到目前为止,我尝试的是:

String pattern = "AABBABA" //or whatever   
String firstChar = pattern.toString().substring(1, 2);
String[] split = pattern.split(firstChar);
for (String string : split) {
    Log.i("findPattern", "Splitted Pattern: " + string + "");
}
我的代码的问题是它删除了所有等于
firstChar
的字符串


我应该使用什么正则表达式将模式拆分为单独的字符串

您可以捕获所需内容,而不是使用此模式拆分

(A+|B+)

我试着按照你的逻辑,得到了这个代码,也许不是你想要的:

String pattern = "AABBABA"; //or whatever   
String firstChar = pattern.toString().substring(1, 2);
String[] split = pattern.split("(?!" + firstChar + ")");
for (String strng : split) 
{
   System.console().writer().println(strng);
}
输出:

AA                                                                                                                                  
B                                                                                                                                   
BA                                                                                                                                  
BA  
AA                                                                                                                                                                  
BB                                                                                                                                                                  
A                                                                                                                                                                   
B                                                                                                                                                                   
A 
[AA, BB, A, B, A]
或者尝试
匹配器

// String to be scanned to find the pattern.
String line = "AABBABA";
String pattern1 = "(A+|B+)";
Pattern r = Pattern.compile(pattern1);
Matcher m = r.matcher(line);
int count = 0;
while(m.find()) 
{
   count++;
   System.console().writer().println(m.group(0));
} 
输出:

AA                                                                                                                                  
B                                                                                                                                   
BA                                                                                                                                  
BA  
AA                                                                                                                                                                  
BB                                                                                                                                                                  
A                                                                                                                                                                   
B                                                                                                                                                                   
A 
[AA, BB, A, B, A]

这背后的思想是,
()\\1+
有助于首先匹配任意数量的重复字符,而这
|。
有助于匹配所有其他单个字符。最后将所有匹配的字符放入一个列表,然后打印出来

   String s = "AABBABA";
   ArrayList<String> fields = new ArrayList<String>();


        Pattern regex = Pattern.compile("(.)\\1+|.");
        Matcher m = regex.matcher(s);
        while(m.find()){

           fields.add(m.group(0));

    }
    System.out.println(fields);
}
通过在数组中定义上述所有输入

   String s[] = {"AA", "ABA", "AABBABA"};
   Pattern regex = Pattern.compile("(.)\\1+|.");
   for(String i:s)
   {
       ArrayList<String> fields = new ArrayList<String>();
        Matcher m = regex.matcher(i);
        while(m.find()){

           fields.add(m.group(0));


    }
        System.out.println(fields);
}

是的,这就是我想要的,我如何在java中实现它?很好,但我的预期输出必须是:“AA”、“BB”、“A”、“B”、“A”@Milad:见更新的答案。花了我很长时间:(非常感谢我现在正在测试;)是的,你的答案对我也很有用,很抱歉我也不能接受这个答案。没关系。谢谢你的投票。非常感谢这正是我想要的