标签:
一:
package cn.sunzn.demo; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Demo { public static void main(String[] args) { System.out.println(isContainChinese("中国China")); } public static boolean isContainChinese(String str) { Pattern p = Pattern.compile("[\u4e00-\u9fa5]"); Matcher m = p.matcher(str); if (m.find()) { return true; } return false; } }
运行结果:
true
二:
package com.renmai.test; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.junit.Test; public class PatternTest { @Test public void test2(){ Pattern pattern = Pattern.compile("f(.+?)k"); Matcher matcher = pattern.matcher("fckfkkfkf"); while(matcher.find()){ String s0 = matcher.group(); String s1 = matcher.group(1); System.out.println(s0 + " ******* " + s1); } System.out.println("---------------------------"); matcher.reset("fucking"); while(matcher.find()){ System.out.println(matcher.group()); } Pattern pattern2 = Pattern.compile("f(.+?)i(.+?)h"); Matcher matcher2 = pattern2.matcher("finishabigfishfrish"); while(matcher2.find()){ String s0 = matcher2.group(); String s1 = matcher2.group(1); String s2 = matcher2.group(2); System.out.println(s0 + " ******* " + s1 + " ------ " + s2 ); } System.out.println("----------------------------------"); Pattern pattern3 = Pattern.compile("19|20\\d\\d([-/.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])"); Matcher matcher3 = pattern3.matcher("1900-01-01 2007/08/13 1900.01.01 1900 01 01 1900-01.01 1900 13 01 1900 02 31"); while(matcher3.find()){ System.out.println(matcher3.group()); } } @Test public void test3(){ Pattern pattern = Pattern.compile("死亡|最美|棚改|省委|跳楼"); String str = "需要匹配的内容"; Matcher matcher = pattern.matcher(str); while(matcher.find()){ System.out.println(matcher.group()); } } }
匹配原则:要求匹配的规则使用的是Unicode编码
标签:
原文地址:http://www.cnblogs.com/a757956132/p/4831310.html