简体   繁体   English

将字符串拆分为重复的字符

[英]Split string into repeated characters

I want to split the string "aaaabbbccccaaddddcfggghhhh" into "aaaa", "bbb", "cccc".我想将字符串“aaaabbbccccaaddddcfggghhhh”拆分为“aaaa”、“bbb”、“cccc”。 "aa", "dddd", "c", "f" and so on. “aa”、“dddd”、“c”、“f”等。

I tried this:我试过这个:

String[] arr = "aaaabbbccccaaddddcfggghhhh".split("(.)(?!\\1)");

But this eats away one character, so with the above regular expression I get "aaa" while I want it to be "aaaa" as the first string.但这会占用一个字符,因此使用上面的正则表达式我得到“aaa”,而我希望它是“aaaa”作为第一个字符串。

How do I achieve this?我如何实现这一目标?

Try this: 尝试这个:

String   str = "aaaabbbccccaaddddcfggghhhh";
String[] out = str.split("(?<=(.))(?!\\1)");

System.out.println(Arrays.toString(out));
=> [aaaa, bbb, cccc, aa, dddd, c, f, ggg, hhhh]

Explanation: we want to split the string at groups of same chars, so we need to find out the "boundary" between each group. 说明:我们想将字符串分成相同字符的组,因此我们需要找出每个组之间的“边界”。 I'm using Java's syntax for positive look-behind to pick the previous char and then a negative look-ahead with a back reference to verify that the next char is not the same as the previous one. 我使用Java语法进行正向后看以选择前一个字符,然后使用负向前看并带有向后引用,以验证下一个字符与前一个字符不同。 No characters were actually consumed, because only two look-around assertions were used (that is, the regular expresion is zero-width). 实际上没有消耗任何字符,因为仅使用了两个环顾断言(即,常规表达式为零宽度)。

What about capturing in a lookbehind? 在后面的捕捉中怎么样?

(?<=(.))(?!\1|$)

as a Java string: 作为Java字符串:

(?<=(.))(?!\\1|$)

here I am taking each character and Checking two conditions in the if loop ie String can't exceed the length and if next character is not equaled to the first character continue the for loop else take new line and print it. 在这里,我接受每个字符,并在if循环中检查两个条件,即String不能超过长度,并且如果下一个字符不等于第一个字符,则继续for循环,否则换行并打印它。

for (int i = 0; i < arr.length; i++) {
    char chr= arr[i];
    System.out.print(chr);
    if (i + 1 < arr.length && arr[i + 1] != chr) {
        System.out.print(" \n");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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