问题描述
需要匹配的文本内容形如:words,"Reference": ["""words""","""words""","""words"""] wordswords中可能包含任意字符,但不包含三引号。我需要抽取[]中的内容,代码如下,结果是没有匹配reg = ""Reference": \[(^(""")*?)]";pat = Pattern.compile(reg);mat = pat.matcher(data);if(mat.find()){System.out.println(mat.group());System.out.println(mat.group(1));}求指点
解决方案
groovy 代码import java.util.regex.Patterntext = "words,"Reference": ["""words1""","""words2""","""words3"""] words "pattern = Pattern.compile(""Reference": \[(.+?)\]")matcher = pattern.matcher(text)while(matcher.find()) { println matcher.group(1)}pattern2 = Pattern.compile(""Reference": \[(("""(.+?)""",?)+)\]")m2 = pattern2.matcher(text)while (m2.find()) { for(i=0;i<=m2.groupCount();++i) { println "group"+i+":"+m2.group(i) }}
时间: 2024-10-22 13:24:01