如何从特殊格式化字符串中提取数据(how to extract data from special formated string)
我有这种格式的字符串
< value1 >,< value2 >, <1,2,3,4,5>,< some value >输出必须是字符串数组:
array[0] = "value1"; array[1] = "value2"; array[2] = "1,2,3,4,5"; array[3] = "some value";任何建议最好的方法是什么?
I have string in this format
< value1 >,< value2 >, <1,2,3,4,5>,< some value >the output must be an array of Strings:
array[0] = "value1"; array[1] = "value2"; array[2] = "1,2,3,4,5"; array[3] = "some value";Any suggestion what is the best way to do that?
最满意答案
一点点的正则魔法怎么样?
public static void main(String[] args) { final String myString = "< value1 >,< value2 >, <1,2,3,4,5>,< some value >"; final Pattern pattern = Pattern.compile("(?<=<)[^>]++(?=>)"); final Matcher m = pattern.matcher(myString); while(m.find()) { System.out.println(m.group().trim()); } }输出:
value1 value2 1,2,3,4,5 some valueHow about a little bit of regex magic?
public static void main(String[] args) { final String myString = "< value1 >,< value2 >, <1,2,3,4,5>,< some value >"; final Pattern pattern = Pattern.compile("(?<=<)[^>]++(?=>)"); final Matcher m = pattern.matcher(myString); while(m.find()) { System.out.println(m.group().trim()); } }Output:
value1 value2 1,2,3,4,5 some value更多推荐
发布评论