Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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_Split - Fatal编程技术网

Java正则表达式查找不同连续字符的位置

Java正则表达式查找不同连续字符的位置,java,regex,split,Java,Regex,Split,我有一些String对象,需要在不同连续字符的位置将它们拆分成一个子字符串数组 我的输入/输出应如下所示: "AAAA" -> ["AAAA"] "AAAABBB" -> ["AAAA", "BBB"] "ABBCCC" -> ["A", "BB", "CCC"] 我希望能够编写一行这样的代码: String[] charRuns = str.split(regex); 其中,str是输入,charRuns是输出,但是regex的值应该是什么?还没有找到一种方法来使

我有一些
String
对象,需要在不同连续字符的位置将它们拆分成一个子字符串数组

我的输入/输出应如下所示:

"AAAA"    -> ["AAAA"]
"AAAABBB" -> ["AAAA", "BBB"]
"ABBCCC"  -> ["A", "BB", "CCC"]
我希望能够编写一行这样的代码:

String[] charRuns = str.split(regex);

其中,
str
是输入,
charRuns
是输出,但是
regex
的值应该是什么?

还没有找到一种方法来使用
split
实现这一点,但是这里有一个基于
模式
匹配器
和迭代的解决方案:

String test = "ABBCCCDDDDE";
//                          | any character, grouped for back-reference
//                          | | immediate back-reference
//                          | |    | 0+ repetition, greedy
Pattern p = Pattern.compile("(.)\\1*");
Matcher m = p.matcher(test);
while (m.find()) System.out.println(m.group());
输出

A
BB
CCC
DDDD
E

目前还无法找到使用
split
执行此操作的方法,但这里有一个基于
Pattern
Matcher
和迭代的解决方案:

String test = "ABBCCCDDDDE";
//                          | any character, grouped for back-reference
//                          | | immediate back-reference
//                          | |    | 0+ repetition, greedy
Pattern p = Pattern.compile("(.)\\1*");
Matcher m = p.matcher(test);
while (m.find()) System.out.println(m.group());
输出

A
BB
CCC
DDDD
E

有一种简单的方法可以做到这一点,根本不需要使用Java正则表达式,下面是它的伪代码:

获取字符串的第一个字符,将其存储在变量firstChar中

count -> 1
startIndex -> 0
create a new arrayList to store the strings.
while(count <= string.length){
   newChar -> string.charAt(count)
   If(newChar != firstChar){
       arrayList.add(string.substring(startIndex, count)
       firstChar = newChar
       startIndex = count
     }
     increment count
  }
计数->1
startIndex->0
创建一个新的arrayList来存储字符串。
while(count string.charAt(count)
If(newChar!=firstChar){
arrayList.add(string.substring)(startIndex,count)
firstChar=newChar
startIndex=计数
}
增量计数
}

一旦在arrayList中有了数据,您就可以对其进行迭代并创建单个数组。

有一种简单的方法可以做到这一点,而无需使用Java正则表达式,下面是它的伪代码:

获取字符串的第一个字符,将其存储在变量firstChar中

count -> 1
startIndex -> 0
create a new arrayList to store the strings.
while(count <= string.length){
   newChar -> string.charAt(count)
   If(newChar != firstChar){
       arrayList.add(string.substring(startIndex, count)
       firstChar = newChar
       startIndex = count
     }
     increment count
  }
计数->1
startIndex->0
创建一个新的arrayList来存储字符串。
while(count string.charAt(count)
If(newChar!=firstChar){
arrayList.add(string.substring)(startIndex,count)
firstChar=newChar
startIndex=计数
}
增量计数
}

一旦你在arrayList中有了数据,你就可以对其进行迭代并创建单独的数组。

可能有用我不会使用regex…@Thomas我会接受任何提供我需要的输入和输出的解决方案,我只希望简单代码使用regex。@oli没有那么有用我不会使用regex…@Thomas我会接受任何解决方案提供我需要的输入和输出的离子,我只希望简单代码使用正则表达式。@这不是一个非常棒的解决方案!一旦我确定
str.split
方法不存在正则表达式,我就会接受它。@4castle谢谢!此解决方案基于正则表达式。但可能有一种方法可以将模式传递到
String\spl它
,尽管您可能需要通过“环顾四周”找到一种方法s、 因为
split
参数代表分隔符,而不是标记。@4castle这是一种冗长的方法。请看@Bohemian的副本是的,它是完全重复的。我现在结束这个问题。这是一个非常棒的解决方案!一旦我确定
str.split
方法不存在正则表达式,我就会接受它。@4castl谢谢!这个解决方案是基于正则表达式的。但是可能有一种方法可以将模式传递到
String#split
,尽管您可能需要通过“环顾四周”找到一种方法s、 由于
split
参数表示分隔符,而不是标记。@4castle这是一种冗长的方法。请参阅@Bohemian的副本是的,它是完全重复的。我现在结束这个问题。