如何从特殊格式化字符串中提取数据(how to extract data from special formated string)

编程入门 行业动态 更新时间:2024-10-27 18:31:18
如何从特殊格式化字符串中提取数据(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 value

How 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

更多推荐

本文发布于:2023-08-07 22:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466172.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   数据   extract   data   formated

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!