정규 표현 식 (2): {} 사이 의 내용 추출

2443 단어 정규 표현 식
참고: 
    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。

좋은 웹페이지 즐겨찾기