Vinu Vish Vinu Vish - 5 months ago 17
Java Question

how to split specific line using regular expression

following string i have

"body":"The Example 1",(sample),{sample},"sample",&sample,"body":"The Example 2",(sample),{sample},"sample",&sample

using above string i need to extract only

  • The Example 1

  • The Example 2

and needs to assign The Example 1 as a string1 The Example 2 as a string 2

Fillowing code im using

Pattern comment1 = Pattern.compile("(?<=\")(?:\\\\.|([\"body\"]))*(?=\")", Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE | Pattern.DOTALL | Pattern.MULTILINE);
Matcher matchComm = comment1.matcher(responce.toString());

i am not getting proper results


This would do the trick:

  • (?<=:") is a possetive look behind for :".
  • (?=",|"$) is a possetive look ahead for ", or "$ where $ marks the end of the line/file.



The following code could be used to produce an array, where every position in the array is a separate match from the input string.

ArrayList<String> out = new ArrayList<String>();
Pattern p = Pattern.compile( {{Pattern}}, Pattern.MULTILINE);
Matcher m = p.matcher( {{String}} );



Since you after writing the question clarified that you indeed wanted to only mach the string following the "body" tag, the following regex would be required instead:


Also if the field following the "body" tag is allowed to be empty, the + should be swapped for a *: