参考 :
https://www.cnblogs.com/mi21/p/10361886.html
https://bbs.youkuaiyun.com/topics/391957642
import java.util.LinkedList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @Auther: liyue
* @Date: 2019/12/5 17:55
* @Description:
*/
public class RegExUtil {
private static final String start = "{";
private static final String end = "}";
private static final String start2 = "[";
private static final String end2 = "]";
private static String s = "[";
private static String e = "]";
public static List<String> extract(String str) {
String patern = "(?<=\\" + start + ")[^\\" + end + "]+";
Pattern pattern = Pattern.compile(patern);
Matcher matcher = pattern.matcher(str);
List<String> result = new LinkedList<>();
while (matcher.find()) {
result.add(matcher.group());
}
return result;
}
public static List<String> extract2(String str) {
String patern = "(?<=\\" + start2 + ")[^\\" + end2 + "]+";
Pattern pattern = Pattern.compile(patern);
Matcher matcher = pattern.matcher(str);
List<String> result = new LinkedList<>();
while (matcher.find()) {
result.add(matcher.group());
}
return result;
}
public static List<String> extract2(String str, String start, String end) {
String patern = "(?<=" + start + ")[^" + end + "]+";
Pattern pattern = Pattern.compile(patern);
Matcher matcher = pattern.matcher(str);
List<String> result = new LinkedList<>();
while (matcher.find()) {
result.add(matcher.group());
}
return result;
}
public static void main(String[] args) {
String str = "说的过124滤{会计}师对24顾{客}数量124212是{否}";
String start = "{", end = "}";
String patern = "(?<=\\" + start + ")[^\\" + end + "]+";
Pattern pattern = Pattern.compile(patern);
Matcher matcher = pattern.matcher(str);
while (matcher.find()) {
System.out.println(matcher.group());
}
}
}
END。
本文详细介绍了一种使用正则表达式从字符串中提取特定格式数据的方法,通过具体实例展示了如何匹配并抓取以特定字符开始和结束的子串,适用于处理包含复杂嵌套结构的文本。
10万+

被折叠的 条评论
为什么被折叠?



