总结了下常用的正则表达式用例,这个经常会用到,有些可能还没有,后边会继续更新,如果文章里有问题请大家指正
import java.util.regex.Matcher; import java.util.regex.Pattern; public class PatternTest { public static void main(String[] args) { //获取“.”以前的所有英文字符 String TestStr = "请访问4555netpay.comjac.com/m?"; String TestStrs = "dafafafafa"; Pattern pattern = Pattern.compile("[a-zA-Z]+"); int sep = TestStr.indexOf("."); String CutStr = null; if (sep >= 0) { CutStr = TestStr.substring(0,sep); Matcher mat = pattern.matcher(CutStr); if (mat.find()) { System.out.println(mat.group()); } } // boolean IfWebsite = Pattern.matches("http://[\\w]+",TestStr); // System.out.print("IfWebsite = "+IfWebsite); System.out.println("CutStr = " + CutStr); //获取类似于这样的字符() Pattern p3 = Pattern .compile("(19|20)\\d\\d([- /.])(0[1-9]|1[012])\\2(0[1-9]|[12][0-9]|3[01])"); Matcher m3 = p3 .matcher("1900-01-01 2007/08/13 1900.01.01 1900 01 01 1900-01.01 1900 13 01 1900 02 31"); while (m3.find()) { System.out.println(m3.group()); } //获取字符串中的所有数字 String TestStr1 = "+10010"; Pattern pattern1 = Pattern.compile("[\\d]+"); Matcher mat = pattern1.matcher(TestStr1); if (mat.find()) { System.out.println(mat.group()); } //判断是否全为数字 String strMobile = "+10010"; if (strMobile.matches("\\d+")) { System.out.println("true"); } } }原文链接:https://www.f2er.com/regex/360910.html