0

I need your help parsing a string that contains an array inside to string array, is it posible? Currently I'm using split to do it but I think that is not the correct way..

Here an example of what my string contains:

["SR RODRIGUEZ SE LO LLEVA  ,,, PIL  4_45  ","COMENTARIOS"]

I hope you can help me guys, I've been stuck with this problem for an entire day..

7
  • 1
    String.split() is actually probably the right way to do it. If you need more help, please show some examples of inputs and expected outputs. Commented Jul 27, 2018 at 15:10
  • can you post the end result you want? Commented Jul 27, 2018 at 15:10
  • This String myString = "["SR RODRIGUEZ SE LO LLEVA ,,, PIL 4_45 ","COMENTARIOS"]"; to this String[] array= {"SR RODRIGUEZ SE LO LLEVA ,,, PIL 4_45 ", "COMENTARIOS"}; System.out.println( array[0] ); System.out.println( array[1] ); Commented Jul 27, 2018 at 15:14
  • Given example at your comment is incorrect, it could not even compile. Provide the working example data set. Commented Jul 27, 2018 at 15:16
  • I'm consuming a WS that returns me a String that contains this: ["SR RODRIGUEZ SE LO LLEVA ,,, PIL 4_45 ","COMENTARIOS"] I want to cast it to String array or something else that allows me to access to every String Commented Jul 27, 2018 at 15:21

2 Answers 2

1

You can achive what you desire with below :

    String myString = "[\"SR RODRIGUEZ SE LO LLEVA ,,, PIL 4_45 \",\"COMENTARIOS\"]";
    Pattern pattern = Pattern.compile("\"(?:\\\\.|[^\"\\\\])*\"");
    Matcher matcher = pattern.matcher(myString);

    List<String> myList = new ArrayList();

    while (matcher.find()) {
        myList.add(matcher.group());
    }

    myList.stream().forEach(System.out::println);

Output :

"SR RODRIGUEZ SE LO LLEVA ,,, PIL 4_45 "
"COMENTARIOS"
Sign up to request clarification or add additional context in comments.

6 Comments

Just to know, this solution works if theres doublequotes (escaped) inside a string?
No, if quotes not escaped, code is not even compile. You can not write a string like String myStr = "["test"]"; If you read your string from external source (file, network etc.) it works. Excepted answer escaped quotes too.
I mean this: ["\"value \"\"", "other \" "] note that it supposed to be two values, you regex capture it correctly?
I paste it into a file and read it from file with String myString = Files.readAllLines(Paths.get("mystr.txt")).get(0); It gives the output : "\"value \"\"" and "other \" " . I dont know is that what u need?
Oh yes, I just wanted to know if it got the escaped quotes or splited it +1 mate, idk regex
|
0

If you want string array of double quoted string. Look at this:

String str = "\"SR RODRIGUEZ SE LO LLEVA  ,,, PIL  4_45  \",\"COMENTARIOS\",\"abc\"";
    String res[] = str.split(",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)");
    Arrays.stream(res).forEach(st -> System.out.println(st));

1 Comment

Just to know, this solution works if theres doublequotes (escaped) inside a string?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.