字符串是否包含特殊字符或中文:
- 是否全中文
- 是否包含特殊字符
- 是否含英文
- 是否全数字
- 字符串替换
案例:
public static void main(String[] args) {
String a = "你好";
String b = "你-aaDS好>(!12340.5";
String c = "你 ";
String d = "aa";
String regex00 = "[\u4e00-\u9fa5]+";
String regex01 = ".*[a-zA-z].*";
String regex02 = "[ .-_`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]|\n|\r|\t";
String regex03 = "[0-9]*";
System.out.println("--------全中文--------------");
System.out.println(a.matches(regex00));
System.out.println(b.matches(regex00));
System.out.println(c.matches(regex00));
System.out.println("--------英文--------------");
System.out.println(a.matches(regex01));
System.out.println(b.matches(regex01));
System.out.println(c.matches(regex01));
System.out.println(d.matches(regex01));
System.out.println("----------特殊字符------------");
Pattern p = Pattern.compile(regex02);
Matcher m = p.matcher(b);
System.out.println(m.find());
System.out.println(b.replaceAll(regex02, ""));
System.out.println("-----------数字-----------");
boolean matches = b.matches(regex03);
System.out.println(matches);
System.out.println("123456".matches(regex03));
System.out.println("你111aaa".matches(regex03));
System.out.println(b.replaceAll(regex03, ""));
}
效果:
D:\software\Java\jdk\jdk1.8\bin\java.exe "-javaagent:D:\software\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=13143:D:\software\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath D:\software\Java\jdk\jdk1.8\jre\lib\charsets.jar;D:\software\Java\jdk\jdk1.8\jre\lib\deploy.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\access-bridge-64.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\cldrdata.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\dnsns.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\jaccess.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\jfxrt.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\localedata.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\nashorn.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\sunec.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\sunjce_provider.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\sunmscapi.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\sunpkcs11.jar;D:\software\Java\jdk\jdk1.8\jre\lib\ext\zipfs.jar;D:\software\Java\jdk\jdk1.8\jre\lib\javaws.jar;D:\software\Java\jdk\jdk1.8\jre\lib\jce.jar;D:\software\Java\jdk\jdk1.8\jre\lib\jfr.jar;D:\software\Java\jdk\jdk1.8\jre\lib\jfxswt.jar;D:\software\Java\jdk\jdk1.8\jre\lib\jsse.jar;D:\software\Java\jdk\jdk1.8\jre\lib\management-agent.jar;D:\software\Java\jdk\jdk1.8\jre\lib\plugin.jar;D:\software\Java\jdk\jdk1.8\jre\lib\resources.jar;D:\software\Java\jdk\jdk1.8\jre\lib\rt.jar;F:\yilianzhong\上传项目\新接口\icon_interface\iconInterface\target\classes;D:\software\Java\maven\repository\org\bouncycastle\bcprov-jdk15to18\1.66\bcprov-jdk15to18-1.66.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-web\1.5.14.RELEASE\spring-boot-starter-web-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter\1.5.14.RELEASE\spring-boot-starter-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-logging\1.5.14.RELEASE\spring-boot-starter-logging-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\ch\qos\logback\logback-classic\1.1.11\logback-classic-1.1.11.jar;D:\software\Java\maven\repository\ch\qos\logback\logback-core\1.1.11\logback-core-1.1.11.jar;D:\software\Java\maven\repository\org\slf4j\jcl-over-slf4j\1.7.25\jcl-over-slf4j-1.7.25.jar;D:\software\Java\maven\repository\org\slf4j\jul-to-slf4j\1.7.25\jul-to-slf4j-1.7.25.jar;D:\software\Java\maven\repository\org\slf4j\log4j-over-slf4j\1.7.25\log4j-over-slf4j-1.7.25.jar;D:\software\Java\maven\repository\org\yaml\snakeyaml\1.17\snakeyaml-1.17.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-tomcat\1.5.14.RELEASE\spring-boot-starter-tomcat-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\apache\tomcat\embed\tomcat-embed-core\8.5.31\tomcat-embed-core-8.5.31.jar;D:\software\Java\maven\repository\org\apache\tomcat\tomcat-annotations-api\8.5.31\tomcat-annotations-api-8.5.31.jar;D:\software\Java\maven\repository\org\apache\tomcat\embed\tomcat-embed-el\8.5.31\tomcat-embed-el-8.5.31.jar;D:\software\Java\maven\repository\org\apache\tomcat\embed\tomcat-embed-websocket\8.5.31\tomcat-embed-websocket-8.5.31.jar;D:\software\Java\maven\repository\org\hibernate\hibernate-validator\5.3.6.Final\hibernate-validator-5.3.6.Final.jar;D:\software\Java\maven\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;D:\software\Java\maven\repository\org\jboss\logging\jboss-logging\3.3.2.Final\jboss-logging-3.3.2.Final.jar;D:\software\Java\maven\repository\com\fasterxml\classmate\1.3.4\classmate-1.3.4.jar;D:\software\Java\maven\repository\com\fasterxml\jackson\core\jackson-databind\2.8.11.2\jackson-databind-2.8.11.2.jar;D:\software\Java\maven\repository\com\fasterxml\jackson\core\jackson-annotations\2.8.0\jackson-annotations-2.8.0.jar;D:\software\Java\maven\repository\com\fasterxml\jackson\core\jackson-core\2.8.11\jackson-core-2.8.11.jar;D:\software\Java\maven\repository\org\springframework\spring-web\4.3.18.RELEASE\spring-web-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-aop\4.3.18.RELEASE\spring-aop-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-beans\4.3.18.RELEASE\spring-beans-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-webmvc\4.3.18.RELEASE\spring-webmvc-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-expression\4.3.18.RELEASE\spring-expression-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-test\5.0.9.RELEASE\spring-test-5.0.9.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-core\4.3.18.RELEASE\spring-core-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot\1.5.14.RELEASE\spring-boot-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-context\4.3.18.RELEASE\spring-context-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-autoconfigure\1.5.14.RELEASE\spring-boot-autoconfigure-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\freemarker\freemarker\2.3.28\freemarker-2.3.28.jar;D:\software\Java\maven\repository\commons-io\commons-io\1.4\commons-io-1.4.jar;D:\software\Java\maven\repository\commons-fileupload\commons-fileupload\1.2.1\commons-fileupload-1.2.1.jar;D:\software\Java\maven\repository\org\apache\poi\poi\3.9\poi-3.9.jar;D:\software\Java\maven\repository\commons-codec\commons-codec\1.10\commons-codec-1.10.jar;D:\software\Java\maven\repository\org\apache\poi\poi-scratchpad\3.9\poi-scratchpad-3.9.jar;D:\software\Java\maven\repository\org\apache\poi\poi-ooxml\3.9\poi-ooxml-3.9.jar;D:\software\Java\maven\repository\org\apache\poi\poi-ooxml-schemas\3.9\poi-ooxml-schemas-3.9.jar;D:\software\Java\maven\repository\org\apache\xmlbeans\xmlbeans\2.3.0\xmlbeans-2.3.0.jar;D:\software\Java\maven\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;D:\software\Java\maven\repository\xml-apis\xml-apis\1.4.01\xml-apis-1.4.01.jar;D:\software\Java\maven\repository\com\google\code\gson\gson\2.3\gson-2.3.jar;D:\software\Java\maven\repository\org\json\json\20080701\json-20080701.jar;D:\software\Java\maven\repository\com\alibaba\fastjson\1.2.7\fastjson-1.2.7.jar;F:\yilianzhong\上传项目\新接口\icon_interface\ylz-jdbc\target\classes;D:\software\Java\maven\repository\mysql\mysql-connector-java\5.1.46\mysql-connector-java-5.1.46.jar;D:\software\Java\maven\repository\com\oracle\ojdbc14-1\10.2.0.4.1\ojdbc14-1-10.2.0.4.1.jar;D:\software\Java\maven\repository\com\alibaba\druid\1.0.20\druid-1.0.20.jar;D:\software\Java\jdk\jdk1.8\lib\jconsole.jar;D:\software\Java\jdk\jdk1.8\lib\tools.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-data-jpa\1.5.14.RELEASE\spring-boot-starter-data-jpa-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-aop\1.5.14.RELEASE\spring-boot-starter-aop-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\aspectj\aspectjweaver\1.8.13\aspectjweaver-1.8.13.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-jdbc\1.5.14.RELEASE\spring-boot-starter-jdbc-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\apache\tomcat\tomcat-jdbc\8.5.31\tomcat-jdbc-8.5.31.jar;D:\software\Java\maven\repository\org\apache\tomcat\tomcat-juli\8.5.31\tomcat-juli-8.5.31.jar;D:\software\Java\maven\repository\org\springframework\spring-jdbc\4.3.18.RELEASE\spring-jdbc-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\hibernate\hibernate-core\5.0.12.Final\hibernate-core-5.0.12.Final.jar;D:\software\Java\maven\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;D:\software\Java\maven\repository\org\jboss\jandex\2.0.0.Final\jandex-2.0.0.Final.jar;D:\software\Java\maven\repository\javax\transaction\javax.transaction-api\1.2\javax.transaction-api-1.2.jar;D:\software\Java\maven\repository\org\springframework\data\spring-data-jpa\1.11.13.RELEASE\spring-data-jpa-1.11.13.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\data\spring-data-commons\1.13.13.RELEASE\spring-data-commons-1.13.13.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-orm\4.3.18.RELEASE\spring-orm-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-tx\4.3.18.RELEASE\spring-tx-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar;D:\software\Java\maven\repository\org\springframework\spring-aspects\4.3.18.RELEASE\spring-aspects-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\hibernate\hibernate-entitymanager\5.0.12.Final\hibernate-entitymanager-5.0.12.Final.jar;D:\software\Java\maven\repository\org\hibernate\common\hibernate-commons-annotations\5.0.1.Final\hibernate-commons-annotations-5.0.1.Final.jar;D:\software\Java\maven\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;D:\software\Java\maven\repository\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;D:\software\Java\maven\repository\org\apache\geronimo\specs\geronimo-jta_1.1_spec\1.1.1\geronimo-jta_1.1_spec-1.1.1.jar;D:\software\Java\maven\repository\org\springframework\boot\spring-boot-starter-data-redis\1.5.14.RELEASE\spring-boot-starter-data-redis-1.5.14.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\data\spring-data-redis\1.8.13.RELEASE\spring-data-redis-1.8.13.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\data\spring-data-keyvalue\1.2.13.RELEASE\spring-data-keyvalue-1.2.13.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-oxm\4.3.18.RELEASE\spring-oxm-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\org\springframework\spring-context-support\4.3.18.RELEASE\spring-context-support-4.3.18.RELEASE.jar;D:\software\Java\maven\repository\redis\clients\jedis\2.9.0\jedis-2.9.0.jar;D:\software\Java\maven\repository\org\apache\commons\commons-pool2\2.4.3\commons-pool2-2.4.3.jar;D:\software\Java\maven\repository\org\apache\httpcomponents\httpclient\4.5.2\httpclient-4.5.2.jar;D:\software\Java\maven\repository\org\apache\httpcomponents\httpcore\4.4.9\httpcore-4.4.9.jar;D:\software\Java\maven\repository\org\apache\httpcomponents\httpmime\4.5.2\httpmime-4.5.2.jar;D:\software\Java\maven\repository\commons-lang\commons-lang\2.6\commons-lang-2.6.jar;D:\software\Java\maven\repository\cn\hutool\hutool-all\5.4.5\hutool-all-5.4.5.jar;D:\software\Java\maven\repository\com\thoughtworks\xstream\xstream\1.4.10\xstream-1.4.10.jar;D:\software\Java\maven\repository\xmlpull\xmlpull\1.1.3.1\xmlpull-1.1.3.1.jar;D:\software\Java\maven\repository\xpp3\xpp3_min\1.1.4c\xpp3_min-1.1.4c.jar;D:\software\Java\maven\repository\org\codehaus\jettison\jettison\1.3.7\jettison-1.3.7.jar;D:\software\Java\maven\repository\stax\stax-api\1.0.1\stax-api-1.0.1.jar com.ylz.vaccineInterface.interfaces.ymupload.UploadOneYMController
--------全中文--------------
true
false
false
--------英文--------------
false
true
false
true
----------特殊字符------------
true
你-aa好
-----------数字-----------
false
true
false
你-aaDS好>(!.
Process finished with exit code 0
|