Home > Back-end >  Regex split string in double quotes
Regex split string in double quotes

Time:09-22

I have the following string I need to split by double quotes. below is the sample string

String raw = "\"AKINWALE JOHNSON ROTIMI                                \"   \"100000072      \"       \"1985"
                  "0217                         \"  \"                                                                                \"      \"0250000066               \""
                  "\"                                                         08035558619\"  \"22324902758    \"       \"NG0250002    \" \"9113985      \"";

When I try to split the string by double quotes, knowing that some quotes can be empty

I tried the following : String[] split = raw.split("\"(\\w\\s |\\s )\"");

it is close but I seem to be missing something.

CodePudding user response:

You can simply split it on \\s*\"\\s* which means " preceded by or followed by zero or more whitespace character(s).

Demo:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String raw = "\"AKINWALE JOHNSON ROTIMI                                \"   \"100000072      \"       \"1985"
                  "0217                         \"  \"                                                                                \"      \"0250000066               \""
                  "\"                                                         08035558619\"  \"22324902758    \"       \"NG0250002    \" \"9113985      \"";

        String[] split = raw.split("\\s*\"\\s*");
        Arrays.stream(split).forEach(System.out::println);
    }
}

Output:

AKINWALE JOHNSON ROTIMI

100000072

19850217



0250000066

08035558619

22324902758

NG0250002

9113985

ONLINE DEMO

  • Related