How to split this string ?
String s="{hello.World::123}";
to Stirng array:开发者_StackOverflow
{"hello","World","123"}
using regex ?
Try this:
String s ="{hello.World::123}";
System.out.println(Arrays.toString(s.split("[\\.:{}]+")));
EDIT:
Generalized solution:
String s ="{hello.World::123}".replaceFirst("^[\\.:{}]+", " ").trim();
System.out.println(Arrays.toString(s.split("[\\.:{}]+")));
From the comments (@Peter Lawrey):
For the particular input,
String s ="{hello.World::123}";
System.out.println(Arrays.toString(s.substring(1).split("[\\.:{}]+")));
Slightly modifying John's solution to avoid empty string:
String s="{hello.World::123}";
s=s.substring(1, s.length()-1);
System.out.println(Arrays.toString(s.split("[\\.:]+")));
Sorry for the inconvenience in my previous answer.
Here is something that may actually work:
String[] a = s.replaceAll("[\\{\\}]", "").split("\\.|::");
However, this will not allow you to have {
or }
as elements in the list, but I guess you don't have that.
I don't think it's a simple, single split. Your struggles might be telling you the same thing.
You can use regex to match on more than one word character and return each group, but I don't believe one call to split will do it.
How about :
s.split("\\.|::");
精彩评论