|
@@ -2,11 +2,9 @@ package com.diagbot.util;
|
|
|
|
|
|
import com.google.common.collect.Lists;
|
|
|
|
|
|
-import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.regex.Matcher;
|
|
|
import java.util.regex.Pattern;
|
|
|
-import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
|
* @author zhoutg
|
|
@@ -179,10 +177,7 @@ public class RegexUtil {
|
|
|
Matcher matcher = pattern.matcher(content);
|
|
|
if (matcher.find()) {
|
|
|
for (int i = 1; i <= matcher.groupCount(); i++) {
|
|
|
- String group = matcher.group(i);
|
|
|
- if(StringUtil.isNotBlank(group)){
|
|
|
- list.add(group);
|
|
|
- }
|
|
|
+ list.add(matcher.group(i));
|
|
|
}
|
|
|
}
|
|
|
} catch (Exception e) {
|
|
@@ -239,30 +234,63 @@ public class RegexUtil {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 测试
|
|
|
+ * 根据正则获取匹配的所有分组数据(公共方法)
|
|
|
*
|
|
|
- * @param args
|
|
|
+ * @param content
|
|
|
+ * @param regex
|
|
|
+ * @return
|
|
|
*/
|
|
|
- public static void main(String[] args) {
|
|
|
- String ff = "(心率)\\s*(\\d+\\.?\\d+|\\d+)(次\\/分)?";
|
|
|
- List<String> regexDataAll = getRegexDataAll("心率23.25,dasfsd心率40次/分", ff);
|
|
|
- System.out.println();
|
|
|
+ public static List<List<String>> getRegexGroupData(String content, String regex) {
|
|
|
+ return getRegexGroupDataCommon(content, regex, false);
|
|
|
+ }
|
|
|
|
|
|
- String str = "1-2-3-4, 法法,0-0-1-0";
|
|
|
- String regex = "(\\d+)-(\\d+)-(\\d+)-(\\d+)";
|
|
|
- System.out.println(getRegexDataList(str, regex));
|
|
|
+ /**
|
|
|
+ * 根据正则获取匹配的所有分组数据(公共方法)(大小写敏感)
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @param regex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static List<List<String>> getRegexGroupDataSen(String content, String regex) {
|
|
|
+ return getRegexGroupDataCommon(content, regex, 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));
|
|
|
+ /**
|
|
|
+ * 根据正则获取匹配的所有分组数据(内部方法)
|
|
|
+ *
|
|
|
+ * @param content
|
|
|
+ * @param regex
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private static List<List<String>> getRegexGroupDataCommon(String content, String regex, Boolean sensitive) {
|
|
|
+ List<List<String>> res = Lists.newArrayList();
|
|
|
+ try {
|
|
|
+ if (StringUtil.isBlank(content)) {
|
|
|
+ return res;
|
|
|
+ }
|
|
|
+ Pattern pattern = getPattern(regex, sensitive);
|
|
|
+ Matcher matcher = pattern.matcher(content);
|
|
|
+ while (matcher.find()) {
|
|
|
+ if (matcher.groupCount() > 0) {
|
|
|
+ List<String> groupList = Lists.newArrayList();
|
|
|
+ for (int i = 1; i <= matcher.groupCount(); i++) {
|
|
|
+ groupList.add(matcher.group(i));
|
|
|
+ }
|
|
|
+ res.add(groupList);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return res;
|
|
|
+ }
|
|
|
|
|
|
- System.out.println(getRegexDataAll("拟诊+(3.3/3.4)任一", "([1-9]\\d*\\.?\\d*)|(0\\.\\d*[1-9])"));
|
|
|
+ /**
|
|
|
+ * 测试
|
|
|
+ *
|
|
|
+ * @param args
|
|
|
+ */
|
|
|
+ public static void main(String[] args) {
|
|
|
|
|
|
- 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);
|
|
|
}
|
|
|
}
|