正则表达式

正则表达式:  是指一个用来描述或者匹配一系列符合某个句法规则的字符串的单个字符串。其实就是一种规则,有自己特殊的应用。

常用的如下:

字符类:

  x x表示由一个字符所组成
  \ \\表示转义字符“\”
  \t              \t(制表符)表示一个“\t”符号
  \n            \n表示换行符号
 
  [abc] a或b或c
  [^abc] 除了a、b、c之外的任意字符
  【a-z】          a-z中的任一字符,小写字母;
  [a-zA-Z] a-zA-Z中的任一字符,不区分大小写
  [0-9]         表示任意的一位数字
 
预定义字符类:
  .  任何一位字符 
  \d  数字:[0-9] 
  \D  非数字:[^0-9] 
  \w  单词字符:[a-zA-Z_0-9] 
  \W  非单词字符:[^\w] 
  \s                   表示任一的空白字符,例如:\t  \n
  \S                表示任一的非空白字符
 
边界匹配器(主要在js中使用):
  ^ 行的开头
  $ 行的结尾
  \b   单词边界
例:
  "I am here".matches("I\\b \\bam\\b \\bhere")
  注意:边界所处的位置。
 
Greedy--数量表达式
  X?  X,一次或一次也没有                   正则?  表示正则出现一次或者零次;
  X*  X,零次、一次或多次 
  X+  X,一次或多次 
  X{n}  X,恰好 n 次 
  X{n,m}  X,至少 n 次,但是不超过 m 次
 
代码示例:
 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 }

获取功能:

Pattern和Matcher类的使用
Pattern类:此类对象如果要想取得必须使用compile()方法,方法的功能是编译正则;
Matcher类:是通过Pattern类取得。
步骤:
  1.通过编译规则得到模式Patterm;
  2.通过模式匹配字符序列得到匹配器;
  3.通过匹配器得到find,在group里获取。
  
Pattern pattern = Pattern.compile(String regex);
Matcher matcher = pattern.matcher(String str);
while(matcher.find()){
String str = matcher.group();
}
 
 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 }
 
 
 
 
 
原文地址:https://www.cnblogs.com/duzhuo/p/5395751.html