正则表达式: 是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则,有自己特殊的应用。
常用的如下:
字符类:
1 package com.ibeifeng.regex; 2 public class RegexDemo2 { 3 public static void main(String[] args) { 4 /* 5 * [abc] a或b或c 6 * [^abc] 除了a、b、c之外的任一字符 7 * [a-zA-Z] a-zA-Z中的任一字符 8 */ 9 System.out.println("d".matches("[abc]")); 10 System.out.println("a".matches("[^abc]")); 11 System.out.println("aa".matches("[a-zA-Z]")); 12 13 /* 14 * \d 15 * "\\"代表一个"\" 16 * \w 0-9a-zA-Z_ 17 */ 18 System.out.println("a".matches("\\d")); 19 System.out.println("~".matches("\\D")); 20 System.out.println("0".matches("\\w")); 21 22 /* 23 * ^ 24 * $ 25 * \b 26 */ 27 System.out.println("acbc".matches("^a\\w\\w\\w")); 28 System.out.println("abcde".matches("\\w\\w\\w\\we$")); 29 30 /* 31 * ? 1次或0次 32 * * 0次或多次 33 * + 1次或多次 34 * {n} 恰好N次 35 * {m,n} 至少m次 至多n次 36 */ 37 System.out.println("carsdff".matches("\\w*")); 38 System.out.println("teacher".matches("\\w+")); 39 System.out.println("abcq".matches("\\w{3}")); 40 System.out.println("==================="); 41 System.out.println("I am here".matches("I\\b \\bam\\b \\bhere")); 42 System.out.println("a".matches(".")); 43 } 44 }
代码示例(/检测邮箱的功能):
1 package com.ibeifeng.regex; 2 public class RegexDemo3 { 3 public static void main(String[] args) { 4 //匹配校验邮箱的正则表达式 5 String s1 = "abcde@163.com"; 6 String s2 = "abcdefg@qq.com"; 7 String s3 = "adfdfw@sina.com.cn"; 8 String s4 = "fwef#com.cn.cn.cn"; 9 /* 10 * . \. 11 * @ \@ 12 */ 13 System.out.println(s4.matches("\\w+\\@\\w+(\\.\\w{2,3}){1,2}")); 14 } 15 }
分割功能:
尝试输入一个年龄 显示是否是在范围内的合适年龄 提示大小
1 package com.ibeifeng.regex; 2 import java.util.Arrays; 3 import java.util.Scanner; 4 /* 5 * public String[] split(String regex) 6 */ 7 public class RegexDemo5 { 8 public static void main(String[] args) { 9 //String age = “18-28”; 10 //尝试输入一个年龄 显示是否是在范围内的合适年龄 提示大小 11 String age = "18-28"; 12 String[] split = age.split("-"); 13 System.out.println(Arrays.toString(split)); 14 int min = Integer.valueOf(split[0]); 15 int max = Integer.valueOf(split[1]); 16 Scanner scanner = new Scanner(System.in); 17 System.out.print("请输入你需要的年龄的对象:"); 18 int nextInt = scanner.nextInt(); 19 if(nextInt > max){ 20 System.out.println("哈哈"); 21 }else if (nextInt < min) { 22 System.out.println("儿童哦"); 23 }else { 24 System.out.println("这里就有你想要的."); 25 } 26 } 27 }
替换功能:
1 package com.ibeifeng.regex; 2 import java.util.Arrays; 3 public class RegexDemo6 { 4 public static void main(String[] args) { 5 6 7 /* 8 * public String replaceAll(String regex, 9 * String replacement) 10 */ 11 //fuck f*** shit s*** cao c** 12 String str5 = "Oh shit! cao! what a fucking weather, what are you fucking doing!"; 13 String replaceAll = str5.replaceAll("fuck", "f***") 14 .replaceAll("shit", "s***") 15 .replaceAll("cao", "c**"); 16 17 System.out.println(replaceAll); 18 19 String str6 = "show me the money 999999"; 20 //数字替换掉 同一替换成* 21 System.out.println(str6.replaceAll("\\d+", "*")); 22 System.out.println(str6.replaceAll("\\d{3}", "*")); 23 } 24 }
获取功能:
1 package com.ibeifeng.regex; 2 import java.util.regex.Matcher; 3 import java.util.regex.Pattern; 4 public class PatternDemo { 5 public static void main(String[] args) { 6 //获取由三个字符组成的单词 7 //You may be out of my sight, but never out of my mind. 8 String s = "You may be out of my sight, but never out of my mind"; 9 //1.根据正则表达式创建模式Pattern 10 Pattern pattern = Pattern.compile("\\b\\w{3}\\b"); 11 //2.通过模式匹配字符序列得到匹配器 12 Matcher matcher = pattern.matcher(s); 13 //3.通过匹配器find,在group里获取 14 int num = 0; 15 while(matcher.find()){ 16 String group = matcher.group(); 17 int start = matcher.start(); 18 int end = matcher.end(); 19 System.out.println(group); 20 System.out.println(start+"==="+end); 21 num ++; 22 } 23 System.out.println(num+"次"); 24 25 /*matcher.find(); 26 String group = matcher.group(); 27 System.out.println(group); 28 29 matcher.find(); 30 group = matcher.group(); 31 System.out.println(group);*/ 32 } 33 }