es应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.6 KiB

7 months ago
  1. package com.bfd.operate.utils;
  2. import lombok.extern.slf4j.Slf4j;
  3. import java.security.MessageDigest;
  4. import java.util.HashSet;
  5. import java.util.Set;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8. /**
  9. * @author jinming
  10. * @version 1.0
  11. * @className StringUtile
  12. * @Date 2022/1/21 11:46
  13. */
  14. @Slf4j
  15. public class StringUtil {
  16. public static boolean hasValue(String str) {
  17. return str != null && !"".equals(str.trim());
  18. }
  19. public static String getRegexGroup(String regex, String str, int id) {
  20. String resultStr = "";
  21. if (hasValue(str)) {
  22. Pattern p = Pattern.compile(regex);
  23. Matcher m = p.matcher(str);
  24. if (m.find()) {
  25. resultStr = m.group(id);
  26. }
  27. }
  28. if ("".equals(resultStr)) {
  29. }
  30. return resultStr;
  31. }
  32. public static Set<String> getEmailAddress(String message) {
  33. Set<String> emailList = new HashSet<>();
  34. Pattern pattern = Pattern.compile("\\w+\\.?\\w+\\@\\w+\\.\\w+");
  35. Matcher m = pattern.matcher(message);
  36. while (m.find()) {
  37. emailList.add(m.group(0));
  38. }
  39. return emailList;
  40. }
  41. public static String getMd5(String string) {
  42. try {
  43. MessageDigest md5 = MessageDigest.getInstance("MD5");
  44. byte[] bs = md5.digest(string.getBytes("UTF-8"));
  45. StringBuilder sb = new StringBuilder(40);
  46. for (byte x : bs) {
  47. if ((x & 0xff) >> 4 == 0) {
  48. sb.append("0").append(Integer.toHexString(x & 0xff));
  49. } else {
  50. sb.append(Integer.toHexString(x & 0xff));
  51. }
  52. }
  53. return sb.toString();
  54. } catch (Exception e) {
  55. //LOG.error("获取md5异常", e);
  56. return "nceaform" + System.currentTimeMillis();
  57. }
  58. }
  59. public static String removeAllHtmlTags(String str) {
  60. return hasValue(str) ? str.replaceAll("<[^<>]+?>", "") : "";
  61. }
  62. public static String getRegexGroup(Pattern regex, String str, int id) {
  63. String resultStr = "";
  64. if (hasValue(str)) {
  65. Matcher m = regex.matcher(str);
  66. if (m.find()) {
  67. resultStr = m.group(id);
  68. }
  69. }
  70. if ("".equals(resultStr)) {
  71. log.error(regex + " parser error!");
  72. }
  73. return resultStr;
  74. }
  75. public static String getStrByPattern(String str, String regex) {
  76. Pattern pattern = Pattern.compile(regex);
  77. Matcher m = pattern.matcher(str);
  78. return m.find() ? m.group(0) : "";
  79. }
  80. }