|
@@ -40,16 +40,12 @@ public class RegexUtil {
|
|
|
/**
|
|
|
* 是否有符合正则的数据(大小写敏感)
|
|
|
*
|
|
|
- * @param content 文本内容
|
|
|
- * @param regex 表达式
|
|
|
- * @param senstive 大小写是否敏感
|
|
|
+ * @param content 文本内容
|
|
|
+ * @param regex 表达式
|
|
|
* @return
|
|
|
*/
|
|
|
- public static Boolean getRegexRes(String content, String regex, boolean senstive) {
|
|
|
- if (senstive) {
|
|
|
- return getRegexResCommon(content, regex, true);
|
|
|
- }
|
|
|
- return getRegexResCommon(content, regex, false);
|
|
|
+ public static Boolean getRegexResSen(String content, String regex) {
|
|
|
+ return getRegexResCommon(content, regex, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -79,7 +75,7 @@ public class RegexUtil {
|
|
|
/**
|
|
|
* 获取pattern
|
|
|
*
|
|
|
- * @param regex 正则表达式
|
|
|
+ * @param regex 正则表达式
|
|
|
* @param sensitive 大小写敏感
|
|
|
* @return
|
|
|
*/
|
|
@@ -94,7 +90,7 @@ public class RegexUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据正则获取指定分组数据(大小写不敏感)
|
|
|
+ * 根据正则获取第一个匹配的指定分组数据(大小写不敏感)
|
|
|
*
|
|
|
* @param content 文本内容
|
|
|
* @param regex 表达式
|
|
@@ -106,22 +102,19 @@ public class RegexUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据正则获取指定分组数据(大小写敏感)
|
|
|
+ * 根据正则获取第一个匹配的指定分组数据(大小写敏感)
|
|
|
*
|
|
|
* @param content 文本内容
|
|
|
* @param regex 表达式
|
|
|
* @param groupNum 获取第几个内容
|
|
|
* @return
|
|
|
*/
|
|
|
- public static String getRegexData(String content, String regex, Integer groupNum, Boolean sensitive) {
|
|
|
- if (sensitive) {
|
|
|
- return getRegexDataCommon(content, regex, groupNum, true);
|
|
|
- }
|
|
|
- return getRegexDataCommon(content, regex, groupNum, false);
|
|
|
+ public static String getRegexDataSen(String content, String regex, Integer groupNum) {
|
|
|
+ return getRegexDataCommon(content, regex, groupNum, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据正则获取指定分组数据(公共方法)
|
|
|
+ * 根据正则获取第一个匹配的指定分组数据(公共方法)
|
|
|
*
|
|
|
* @param content 文本内容
|
|
|
* @param regex 表达式
|
|
@@ -148,38 +141,35 @@ public class RegexUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据正则获取所有分组数据(大小写不敏感)
|
|
|
+ * 根据正则获取第一个匹配的所有分组数据(大小写不敏感)
|
|
|
*
|
|
|
* @param content
|
|
|
* @param regex
|
|
|
* @return
|
|
|
*/
|
|
|
- public static List<String> getRegexData(String content, String regex) {
|
|
|
- return getRegexDataCommon(content, regex, false);
|
|
|
+ public static List<String> getRegexDataList(String content, String regex) {
|
|
|
+ return getRegexDataListCommon(content, regex, false);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据正则获取所有分组数据(大小写敏感)
|
|
|
+ * 根据正则获取第一个匹配的所有分组数据(大小写敏感)
|
|
|
*
|
|
|
* @param content
|
|
|
* @param regex
|
|
|
* @return
|
|
|
*/
|
|
|
- private static List<String> getRegexData(String content, String regex, Boolean sensitive) {
|
|
|
- if (sensitive) {
|
|
|
- getRegexDataCommon(content, regex, true);
|
|
|
- }
|
|
|
- return getRegexDataCommon(content, regex, false);
|
|
|
+ public static List<String> getRegexDataListSen(String content, String regex) {
|
|
|
+ return getRegexDataListCommon(content, regex, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 根据正则获取所有分组数据(内部方法)
|
|
|
+ * 根据正则获取第一个匹配的所有分组数据(内部方法)
|
|
|
*
|
|
|
* @param content
|
|
|
* @param regex
|
|
|
* @return
|
|
|
*/
|
|
|
- private static List<String> getRegexDataCommon(String content, String regex, Boolean sensitive) {
|
|
|
+ private static List<String> getRegexDataListCommon(String content, String regex, Boolean sensitive) {
|
|
|
List<String> list = Lists.newArrayList();
|
|
|
try {
|
|
|
if (StringUtil.isBlank(content)) {
|
|
@@ -198,18 +188,51 @@ public class RegexUtil {
|
|
|
return list;
|
|
|
}
|
|
|
|
|
|
- public static List<String> getRegexDatas(String content, String pattern){
|
|
|
- List<String> numbers=new ArrayList<>();
|
|
|
- // 创建 Pattern 对象
|
|
|
- Pattern r = Pattern.compile(pattern);
|
|
|
- // 现在创建 matcher 对象
|
|
|
- Matcher matcher = r.matcher(content);
|
|
|
- while (matcher.find()) {
|
|
|
- //获取当前匹配的值
|
|
|
- numbers.add(matcher.group());
|
|
|
- }
|
|
|
+ /**
|
|
|
+ * 根据正则获取所有匹配数据(大小写不敏感)
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @param regex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getRegexDataAll(String content, String regex) {
|
|
|
+ return getRegexDataAllCommon(content, regex, false);
|
|
|
+ }
|
|
|
|
|
|
- return numbers;
|
|
|
+ /**
|
|
|
+ * 根据正则获取所有匹配数据(大小写敏感)
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @param regex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<String> getRegexDataAllSen(String content, String regex) {
|
|
|
+ return getRegexDataAllCommon(content, regex, true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据正则获取所有匹配数据(内部方法)
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @param regex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static List<String> getRegexDataAllCommon(String content, String regex, Boolean sensitive) {
|
|
|
+ List<String> list = Lists.newArrayList();
|
|
|
+ try {
|
|
|
+ if (StringUtil.isBlank(content)) {
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ Pattern pattern = getPattern(regex, sensitive);
|
|
|
+ Matcher matcher = pattern.matcher(content);
|
|
|
+ while (matcher.find()) {
|
|
|
+ //获取当前匹配的值
|
|
|
+ list.add(matcher.group());
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return list;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -218,19 +241,21 @@ public class RegexUtil {
|
|
|
* @param args
|
|
|
*/
|
|
|
public static void main(String[] args) {
|
|
|
- String regex1 = "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])";
|
|
|
- // System.out.println(getRegexData("血小板计数 30.3", regex1, 2));
|
|
|
- System.out.println(getRegexData("拟诊+(3.3/3.4)任一", regex1));
|
|
|
+ String str = "1-2-3-4, 法法,0-0-1-0";
|
|
|
+ String regex = "(\\d+)-(\\d+)-(\\d+)-(\\d+)";
|
|
|
+ System.out.println(getRegexDataList(str, regex));
|
|
|
|
|
|
- String s1 = "ABC";
|
|
|
- System.out.println(getRegexRes(s1, "Abc", true));
|
|
|
+ // String regex1 = "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])";
|
|
|
+ // // System.out.println(getRegexData("血小板计数 30.3", regex1, 2));
|
|
|
+ // System.out.println(getRegexData("拟诊+(3.3/3.4)任一", regex1));
|
|
|
+ //
|
|
|
+ // String s1 = "ABC";
|
|
|
+ // System.out.println(getRegexRes(s1, "Abc", true));
|
|
|
|
|
|
- System.out.println(getRegexDatas("拟诊+(3.3/3.4)任一","([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])"));
|
|
|
+ System.out.println(getRegexDataAll("拟诊+(3.3/3.4)任一", "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])"));
|
|
|
|
|
|
ArrayList<String> js = Lists.newArrayList("G.4", "G.1", "G.2", "G.3");
|
|
|
List<String> collect = js.stream().sorted().collect(Collectors.toList());
|
|
|
System.out.println(collect);
|
|
|
-
|
|
|
-
|
|
|
}
|
|
|
}
|