测试字符串
String str = "[\"内容\",\"标题\"]";
编码如下:
package Action; import java.util.regex.Matcher; import java.util.regex.Pattern; public class demo { public static void main(String[] args) { String str = "[\"内容\",\"标题\"]"; Pattern p = Pattern.compile("\"(.*?)\""); Matcher m=p.matcher(str); while(m.find()){ System.out.println(m.group()); } } }
效果:
这是有双引号的,咱们再去掉一下,并且编辑成一个函数
package Action; import java.util.LinkedList; import java.util.regex.Matcher; import java.util.regex.Pattern; public class demo { public static void main(String[] args) { String str = "[\"内容\",\"标题\"]"; LinkedList<String> getStr = GetStr(str); for (String s : getStr) { System.out.println(s); } } /** * 给定字符串,拆分后返回集合 * @param str * @return */ public static LinkedList<String> GetStr(String str) { LinkedList<String> l=new LinkedList<String>(); Pattern p = Pattern.compile("\"(.*?)\""); Matcher m=p.matcher(str); while(m.find()){ l.add(m.group().replace("\"", "")); } return l; } }