All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.suyeer.basic.util.RegExUtil Maven / Gradle / Ivy
package com.suyeer.basic.util;
import com.suyeer.basic.enums.RegExTypeEnum;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* @author jun 2019/1/15.
*/
public class RegExUtil {
public static List getLeastMatcherList(String srcStr, String startStr, String endStr) {
return getLeastMatcherList(srcStr, startStr, endStr, RegExTypeEnum.WITH_START_WITH_END);
}
public static List getLeastMatcherList(String srcStr, String startStr, String endStr, RegExTypeEnum regExType) {
List retList = new ArrayList<>();
String[] arr = srcStr.split(startStr);
String regex = String.format(regExType.getRegex(), changeSpecialWord(startStr), changeSpecialWord(endStr));
for (int i = 0; i < arr.length; i++) {
String tmpSrc = startStr + arr[i] + startStr;
if (i == 0) {
tmpSrc = arr[i] + startStr;
}
if (i == (arr.length - 1)) {
tmpSrc = startStr + arr[i];
}
retList.addAll(getMatcherList(tmpSrc, regex));
}
return retList;
}
/**
* 获取正则匹配的 string 数组
*
* @param srcStr String
* @param startStr String
* @param endStr String
* @return List
*/
public static List getMatcherList(String srcStr, String startStr, String endStr) {
return getMatcherList(srcStr, startStr, endStr, RegExTypeEnum.WITH_START_WITH_END);
}
/**
* 获取正则匹配的 string 数组
*
* @param srcStr String
* @param startStr String
* @param endStr String
* @param regExType RegExTypeEnum
* @return List
*/
public static List getMatcherList(String srcStr, String startStr, String endStr, RegExTypeEnum regExType) {
String regex = String.format(regExType.getRegex(), changeSpecialWord(startStr), changeSpecialWord(endStr));
return getMatcherList(srcStr, regex);
}
/**
* 获取正则匹配的 string 数组
* 特殊字符 '$', '(', ')', '*', '+', '.', '[', ']', '?', '\\', '^', '{', '}', '|'
*
* @param srcStr string
* @param regex string 若包含特殊字符, 需要添加双斜杠转译
* @return List
*/
public static List getMatcherList(String srcStr, String regex) {
List retList = new ArrayList<>();
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(srcStr);
while (matcher.find()) {
retList.add(matcher.group());
}
return retList;
}
private static String changeSpecialWord(String str) {
try {
return str.replace("\\", "\\\\").replace("$", "\\$")
.replace("(", "\\(").replace(")", "\\)")
.replace("*", "\\*").replace("+", "\\+")
.replace(".", "\\.").replace("?", "\\?")
.replace("^", "\\^").replace("|", "\\|")
.replace("{", "\\{").replace("}", "\\}")
.replace("[", "\\[").replace("]", "\\]");
} catch (Exception e) {
LogUtil.error(e);
}
return StringUtils.EMPTY;
}
}