All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.util.lang.RegexUtil Maven / Gradle / Ivy

The newest version!
package com.datastax.util.lang;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexUtil {
    public static void main(String[] args) {
        test3();
    }

    private static void test1() {
        String text = "(表B)测试";
        //System.out.println(replace("\\(.*\\)",text,""));
        System.out.println(ExtractByPattern(text, "(\\()(.+?)(\\)测试)", 2));
    }

    private static void test2() {
        String text = "A<-X->Cn";
        String pattern = "(.+?)(?:-|<-)(.+?)(?:->|-)(.*)";
        List list = ExtractByPatternAll(text, pattern);
        System.out.print(list);
    }

    private static void test3(){
        String url="http://www.xxx.com/{1-1000}.html";
        String pattern="(\\{)(.+?)(-)(.+?)(\\})";
        List list = ExtractByPatternAll(url, pattern);
        System.out.print(list);
    }

    public static List ExtractByPatternAll(String text, String pattern) {
        if (text == null) return null;

        List list = new ArrayList();
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(text);

        while (m.find()) {
            for (int i = 1; i <= m.groupCount(); i++) {
                list.add(m.group(i));
            }
        }
        return list;
    }

    public static List ExtractByPattern(String text, String pattern, int group) {
        if (text == null) return null;

        List list = new ArrayList();
        Pattern p = Pattern.compile(pattern);
        Matcher m = p.matcher(text);

        while (m.find()) {
            list.add(m.group(group));
        }
        return list;
    }

    public static List ExtractByPattern(String text, String pattern) {
        return ExtractByPattern(text, pattern, 1);
    }

    public static String filter(String str, String reg) {
        Pattern pattern = Pattern.compile(reg);
        Matcher matcher = pattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
            matcher.appendReplacement(sb, "");
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String replace(String regex, String text, String replaceText) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        StringBuffer sb = new StringBuffer();
        boolean result1 = matcher.find();
        while (result1) {
            matcher.appendReplacement(sb, replaceText);
            result1 = matcher.find();
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static boolean matches(String regex, String text) {
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(text);
        return matcher.matches();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy