正则表达式(2) : 提取{}之间的内容

 参考 : 

    https://www.cnblogs.com/mi21/p/10361886.html

    https://bbs.csdn.net/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 extract(String str) {
        String patern = "(?<=\\" + start + ")[^\\" + end + "]+";
        Pattern pattern = Pattern.compile(patern);
        Matcher matcher = pattern.matcher(str);
        List result = new LinkedList<>();
        while (matcher.find()) {
            result.add(matcher.group());
        }
        return result;
    }

    public static List extract2(String str) {
        String patern = "(?<=\\" + start2 + ")[^\\" + end2 + "]+";
        Pattern pattern = Pattern.compile(patern);
        Matcher matcher = pattern.matcher(str);
        List result = new LinkedList<>();
        while (matcher.find()) {
            result.add(matcher.group());
        }
        return result;
    }


    public static List extract2(String str, String start, String end) {
        String patern = "(?<=" + start + ")[^" + end + "]+";
        Pattern pattern = Pattern.compile(patern);
        Matcher matcher = pattern.matcher(str);
        List 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。

你可能感兴趣的:(正则表达式)