正则表达式的规则及应用

第三阶段 JAVA常见对象的学习

正则表达式

(一) 正则表达式概述

(1) 简单概述

就是符合一定规则的字符串、

(2) 常见规则

  
A:字符

x 字符 x。举例:'a'表示字符a
\ 反斜线字符。
\n 新行(换行)符 ('\u000A')
\r 回车符 ('\u000D')


B:字符类

[abc] a、b c(简单类)
[^abc] 任何字符,除了 a、b c(否定)
[a-zA-Z] a到z A到Z,两头的字母包括在内(范围)
[0-9] 0到9的字符都包括


C:预定义字符类

. 任何字符。我的就是.字符本身,怎么表示呢 \.
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]
在正则表达式里面组成单词的东西必须有这些东西组成


D:边界匹配器

^ 行的开头
$ 行的结尾
\b 单词边界
就是不是单词字符的地方。
举例:hello world?haha;xixi



E:Greedy 数量词

X? X,一次或一次也没有
X* X,零次或多次
X+ X,一次或多次
X{n} X,恰好 n
X{n,} X,至少 n
X{n,m} X,至少 n 次,但是不超过 m

(3) 常见功能

  
//判断功能
String类的public boolean matches(String regex)
   
//分割功能
String类的public String[] split(String regex)
   
//替换功能
String类的public String replaceAll(String regex,String replacement)
   
//获取功能
Pattern和Matcher
Pattern p = Pattern.compile("a*b");
Matcher m = p.matcher("aaaaab");

find():查找存不存在
group():获取刚才查找过的数据

正则表达式是非常强大的,我们通过几个简单的例子来看一下正则表达式的应用

(二) 正则表达式的应用

(1) 判断功能以及正则表达式——验证邮箱格式案例

  
import java.util.Scanner;

public class RegexDemo {
   public static void main(String[] args) {
       Scanner sc = new Scanner(System.in);
       System.out.println("请输入你的邮箱");
       String email = sc.nextLine();

       //定义邮箱规则
       //"[a-zA-Z_0-9]+@[a-zA-Z_0-9]{2,6}(\\.[a-zA-Z_0-9]{2,3})+"
       //简化后
       String regex = "\\w+@\\w{2,6}(\\.\\w{2,3}+)";
       boolean flag = email.matches(regex);
       System.out.println(flag);
  }
}

(2) 拆分功能——好友年龄范围限制

  
import java.util.Scanner;

/*
* 分割功能
*         String类的 public String[] split(String regex)
*         根据正则表达式的匹配拆分此字符串
* 举例:
*         社交软件中
*         搜索好友:
*             性别:女
*             年龄:18-24
*/
public class RegexDemo {
   public static void main(String[] args) {
       String ages = "18-24";

       //定义规则
       String regex = "-";

       //调用方法
       String[] strArray = ages.split(regex);

       //得到int类型
       int StartAge = Integer.parseInt(strArray[0]);
       int EndAge = Integer.parseInt(strArray[1]);

       Scanner sc = new Scanner(System.in);
       System.out.println("请输入你的年龄");
       int age = sc.nextInt();
       if (age >= StartAge && age <= EndAge){
           System.out.println("确认过眼神,我遇上对的人!");
      }else{
           System.out.println("可惜不是你,陪我到最后!");
      }
  }
}

(3) 把字符串中的数字排序

  
import java.util.Arrays;

public class RegexDemo2 {
   public static void main(String[] args) {
       String s = "22 33 55 88 66 11";
       String[] strArray = s.split(" ");

       //把字符串数字转换成int数组
       int[] arr = new int[strArray.length];
       for (int x = 0; x < arr.length; x++) {
           arr[x] = Integer.parseInt(strArray[x]);
      }
       Arrays.sort(arr);

       //把排序后的int数组再组装成一个字符串
       StringBuilder sb = new StringBuilder();
       for (int x = 0; x < arr.length; x++) {
           sb.append(arr[x]).append(" ");
      }

       String result = sb.toString().trim();
       System.out.println("result: " + result);
  }
}

//运行结果
result: 11 22 33 55 66 88

(4) 替换功能

  
package cn.bwh_03_RegexReplaceAll;

public class RegexDemo {
   public static void main(String[] args) {
       String s1 = "hello123456world";

       //所有数字用*给替换
       String regex = "\\d";
       String s2 = "*";

       String result = s1.replaceAll(regex, s2);
       System.out.println(result);
  }
}

//运行结果
hello******world

(5) 获取字符串中由3个字符组成的单词

  import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexDemo {
   public static void main(String[] args) {
       String s = "hao hao xue xi tian tian xiang shang";

       //规则
       String regex = "\\b\\w{3}\\b";

       //把规则编译成模式对象
       Pattern p = Pattern.compile(regex);
       //通过模式对象得到匹配器对象
       Matcher m = p.matcher(s);

       while (m.find()){
           System.out.println(m.group());
      }
  }
}

//运行结果
hao
hao
xue

结尾:

如果内容中有什么不足,或者错误的地方,欢迎大家给我留言提出意见, 蟹蟹大家 !^_^

如果能帮到你的话,那就来关注我吧!(系列文章均会在公众号第一时间更新)

在这里的我们素不相识,却都在为了自己的梦而努力 ❤

一个坚持推送原创Java技术的公众号:理想二旬不止

猜你喜欢

转载自www.cnblogs.com/ideal-20/p/11018401.html
今日推荐