简体   繁体   中英

Split string without losing split character

I want to split a string in Java some string like this, normal split function splits the string while losing the split characters:

String = "123{456]789[012*";

I want to split the string for {,[,],* character but don't want to lose them. I mean I want results like this:

part 1 = 123{
part 2 = 456]
part 3 = 789[
part 4 = 012*

Normally split function splits like this:

part 1 = 123
part 2 = 456
part 3 = 789
part 4 = 012

Is it possible?

Using a positive lookbehind :

(?<={|\[|\]|\*)

String str = "123{456]789[012*";
String parts[] = str.split("(?<=\\{|\\[|\\]|\\*)");
System.out.println(Arrays.toString(parts));

Output:

[123{, 456], 789[, 012*]

I think you're looking for something like

String str = "123{456]789[012*";
String[] parts = new String[] {
        str.substring(0,4), str.substring(4,8), str.substring(8,12),
        str.substring(12)
};
System.out.println(Arrays.toString(parts));

Output is

[123{, 456], 789[, 012*]

You can use zero-width lookahead/behind expressions to define a regular expression that matches the zero-length string between one of your target characters and anything that is not one of your target characters:

(?<=[{\[\]*])(?=[^{\[\]*])

Pass this expression to String.split :

String[] parts = "123{456]789[012*".split("(?<=[{\\[\\]*])(?=[^{\\[\\]*])");

If you have a block of consecutive delimiter characters this will split once at the end of the whole block, ie the string "123{456][789[012*" would split into four blocks "123{", "456][", "789[", "012*" . If you used just the first part (the look-behind)

(?<=[{\[\]*])

then you would get five parts "123{", "456]", "[", "789[", "012*"

You can use a PatternMatcher to find the next index after a splitting character and the splitting character itself.

public static List<String> split(String string, String splitRegex) {
    List<String> result = new ArrayList<String>();

    Pattern p = Pattern.compile(splitRegex);
    Matcher m = p.matcher(string);
    int index = 0;
    while (index < string.length()) {
        if (m.find()) {
            int splitIndex = m.end();
            String splitString = m.group();
            result.add(string.substring(index,splitIndex-1) + splitString);
            index = splitIndex;
        } else
            result.add(string.substring(index));
    }
    return result;
}

Example code:

public static void main(String[] args) {
    System.out.println(split("123{456]789[012*","\\{|\\]|\\[|\\*"));
}

Output:

[123{, 456], 789[, 012*]

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