正则表达式

Demo

  • 需求:判断一个QQ号码格式是否正确
    * 1:全数字
    * 2:开头不能为0
    * 3:长度在5-12
    *
    * 判断电话号码格式是否正确?
    * 11位
    *
    * 判断条件过多,建议使用正则表达式来解决!
    *
import java.util.Scanner;

public class Demo2 {

	public static void main(String[] args) {
		
	
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入qq号码");
		String qq = sc.nextLine();
		//一个具有正确规则字符串表达式
		String regex = "[1-9][0-9]{4,14}";
		
	    boolean flag = qq.matches(regex);
		
		
		if(flag){
			System.out.println("qq格式正确");
		}else{
			System.out.println("qq格式不正确");
		}
		
	}

}

正则表达式的规则组成

A:字符

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

B:字符类

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

C:预定义字符类

. 任何字符。我的就是.字符本身,怎么表示呢? .
\d 数字:[0-9]
\w 单词字符:[a-zA-Z_0-9]

D:边界匹配器

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

E:Greedy 数量词

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


public class Demo {
 static void main(String[] args) {
		String s = "1234567";
		String regex = "[0-9]{3,6}";
		System.out.println(s.matches(regex));
					}

}

import java.util.Scanner;

public class Demo2 {
	
	/*
	 * 判断电话号码是否合格
	 * 1:首位为1
	 * 2:长度11位
	 * 3:第二位 3,4,5,6,7,8,9
	 * 4:后面随便
	 * 
	 */
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入电话号码");
		String phone = sc.nextLine();
		//写电话号码的规则
		String regex = "1[3-9][0-9]{9}";
		
		boolean flag = phone.matches(regex);
		if(flag){
			System.out.println("电话格式正确");
		}else{
			System.out.println("电话格式不正确");
		}
	}

}

import java.util.Scanner;

public class Demo3 {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		System.out.println("请输入email号码");
		String email = sc.nextLine();
		//写email号码的规则
		//String regex = "[a-zA-Z_0-9]{6,15}@[a-zA-Z_0-9]{2,5}(\\.[a-zA-Z_0-9]{2,5})+";
		
		String regex = "\\w{6,15}@\\w{2,5}(\\.\\w{2,5})+";
		boolean flag = email.matches(regex);
		if(flag){
			System.out.println("email格式正确");
		}else{
			System.out.println("email格式不正确");
		}
		
	}

}

正则表达式的分割功能

  • split()
    *
    * 分割功能:
    * aa,bb,cc
    * aa.bb.cc
    * aa bb cc
    * D:\java1129\第二十三天-XML解析\视频
    * 注意:路径分割 ,一个\代表2个\

public class Demo {
	public static void main(String[] args) {
		String str = "D:\\java1129\\第二十三天-XML解析\\视频";
		String regex = "\\\\";
		String[] split = str.split(regex);
		
		for (String s : split) {
			System.out.println(s);
		}
		
	}

}

  • 我有如下一个字符串:”91 27 46 38 50”
    * 请写代码实现最终输出结果是:”27 38 46 50 91”
    * 步骤
    * 1:按照" +"分割字符串 ,返回一个字符串数组
    * 2:把每一个字符串转为整数
    * 3:对整数进行从小到大排序
    * 4:最后再把排好序的数子,拼接成字符串

import java.util.Arrays;

public class Demo2 {
	public static void main(String[] args) {
		String str = "91 27  46  38  50";
		//1:按照" +"分割字符串 ,返回一个字符串数组
		String regex = " +";
		String[] split = str.split(regex);
		//2创建一个整型数组,长度为split数组的长度
		int[] arr = new int[split.length];
		//3:把每一个字符串转为整数
		for (int i = 0;i<split.length;i++) {
			arr[i] = Integer.parseInt(split[i]);
		}
		//4:数组排序
		Arrays.sort(arr);
		//5:遍历整型数组
		String s1 = "";
		StringBuffer sb = new StringBuffer();
		for (int i : arr) {
			sb.append(i).append(" ");
		}
		System.out.println("原始字符串:"+str);
		System.out.println("分割排序字符串:"+sb.toString());
	}

}

正则表达式的替换功能

  • replaceAll(String regex, String replacement)
    * 使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。
    * 论坛中不能出现数字字符,用*替换
public class Demo {
	public static void main(String[] args) {
		String str = "你好吗这是我的银行账号 12456789";
		String regex = "[0-9]";
		
		str = str.replaceAll(regex, "*");
		System.out.println(str);
			
	}
}

正则表达式的获取功能

  • 获取由三个字符组成的单词
    * String str = “da jia ting wo shuo, jin tian yao xia yu. bu shang wan zi xi, gao xing bu?”
    * 使用场合:网站爬虫
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo {
	public static void main(String[] args) {
		String str = "da jia ting wo shuo, jin tian yao xia yu. bu shang wan zi xi, gao xing bu?";
		String regex = "\\b[a-z]{3}\\b"; //非单词+3个字母+非单词
		//1:获取Pattern实例,按着\\b[a-z]{3}\\b 
		Pattern pt = Pattern.compile(regex);
		//2:获取匹配器
		Matcher matcher = pt.matcher(str);
		//3:根据匹配器,先判断是否有3个字符的构成的单词
		while(matcher.find()){
			//4:获取3个字母构成的单词
			System.out.println(matcher.group());
		}
		
		//注意:获取之前必须判断  ,先调用find 再调用 group
		
	}

}


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Demo2 {
	
	
	public static void main(String[] args) throws IOException {
		String regex = "1[3-9][0-9]{9}";
		Pattern p = Pattern.compile(regex);
	   //读取文件
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
		String str = null;
		while((str = br.readLine())!=null){
			Matcher matcher = p.matcher(str);
			while(matcher.find()){
				System.out.println(matcher.group());
			}
			
		}
	}

}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值