|
@@ -0,0 +1,67 @@
|
|
|
+package com.museum.common.util;
|
|
|
+
|
|
|
+import org.junit.Test;
|
|
|
+
|
|
|
+import java.util.regex.Matcher;
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * Created by owen on 2020/3/19 0019 15:45
|
|
|
+ *
|
|
|
+ * 正则表达式
|
|
|
+ */
|
|
|
+public class RegexUtils {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /** 只允许中文 */
|
|
|
+ static final String isChinese = "^[一-\u9fff]+$";
|
|
|
+
|
|
|
+ public static void main(String[] args) {
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 是否包含中文字符 */
|
|
|
+ private static boolean isContainChinese(String param) {
|
|
|
+ Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
|
|
|
+ Matcher m = p.matcher(param);
|
|
|
+ return m.find();
|
|
|
+ }
|
|
|
+
|
|
|
+ /** 只允许中文字符 */
|
|
|
+ private static boolean isChinese(String param) {
|
|
|
+ return param.matches(isChinese);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /** 去除特殊字符 */
|
|
|
+ @Test
|
|
|
+ public void test1(){
|
|
|
+// String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]";
|
|
|
+ String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]";
|
|
|
+
|
|
|
+ String str = "我是 \' 中国(人),你在{干嘛}-哈哈。dddd.jpg";
|
|
|
+ System.out.println(str.replaceAll(regEx, ""));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ public static String sqlValid(String str){
|
|
|
+ if (str.isEmpty() || str.length() > 10000) {
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+ String regEx="[\n`~!@#$%^&*()+=|{}':;',\\[\\]<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。, 、?]";
|
|
|
+ return str.replaceAll(regEx, "");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void test2(){
|
|
|
+
|
|
|
+ String str = "我是 \' 中国(人),你在{干嘛}-哈哈。dddd.jpg";
|
|
|
+ System.out.println(sqlValid(str));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+}
|