I have a string of form "Some lengthy string, Pattern1: string1, Pattern2: string2, Pattern3: string3"
. My objective is to extract string1
, string2
and string3
from such a String
.
Note that the Strings
, Pattern1
, Pattern2
and Pattern3
themselves consist of multiple characters. I know the solution if these patterns were single characters. Currently I am forced to do multiple String.split()
calls one each for Pattern1
, Pattern2
and Pattern3
.
Is there a more graceful way of doing this? Or there is no escaping multiple split()
calls?
EDIT: I was looking or a java specific solution (Hence Java was in title, but it got edited out). Any way. This is what I tried and it appears to work for me.
String someString = "Some lengthy string Pattern1: string1 Pattern2: string2 Pattern3: string3";
String [] str = someString.split("Pattern1:|Pattern2:|Pattern3:");
System.out.println(str[1]);
System.out.println(str[2]);
System.out.println(str[3]);
Output of this is
string1
string2
string3
Any obvious problems with this?