Java基础(四十九)-常用类库

正则表达式

背景

通过之前一系列的分析可以看见,String是一个非常万能的类型,因为String不仅仅可以支持有各种字符串的处理操作,也支持有向各个数据类型的转换功能,所以在项目的开发之中,只要是用户输入的信息基本都是String表示。于是在向其他数据转换的时候,为了保证转换的正确性,往往需要对其进行一些复杂的验证处理,那么这种情况下如果只是单纯的依靠String类中的方法是非常麻烦的。

1:什么是正则表达式

在这里插入图片描述


public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "123" ;
		if (isNumber(str)) {
			int num = Integer.parseInt(str) ;
			System.out.println(num * 2);
		}
	}
	public static boolean isNumber(String str) {
		char data [] = str.toCharArray() ;
		for (int x = 0 ; x < data.length ; x ++) {
			if (data[x] > '9' || data[x] < '0') {
				return false ;
			}
		}
		return true ;
	}
}


public class Test{
	public static void main(String[] args) throws Exception {
		String str = "123" ;
		if (isNumber(str)) {
			int num = Integer.parseInt(str) ;
			System.out.println(num * 2);
		}
	}
	public static boolean isNumber(String str) {
		char data [] = str.toCharArray() ;
		for (int x = 0 ; x < data.length ; x ++) {
			if (data[x] > '9' || data[x] < '0') {
				return false ;
			}
		}
		return true ;
	}
}

在这里插入图片描述

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "123" ;
		if (str.matches("\\d+")) {
			int num = Integer.parseInt(str) ;
			System.out.println(num * 2);
		}
	}
}

正则表达式在JDK1.4的时候需要使用到正则表达式需要单独引入*.jar文件,但是从JDK1.4之后,正则已经默认被JDK支持,提供有java.util.regex开发包,同时针对String类也进行了一些修改,使其可以有方法直接支持正则处理。

2:需要背的正则标记
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

3:String类对正则的支持

在进行正则表达式大部分处理的情况下都会基于String类完成,有如下方法:

在这里插入图片描述

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "a" ;	// 要判断的数据
		String regex = "a" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "c" ;	// 要判断的数据
		String regex = "[abc]" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true
public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "1" ;	// 要判断的数据
		String regex = "[a-zA-Z]" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//false
public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "1" ;	// 要判断的数据
		String regex = "[0-9]" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "#" ;	// 要判断的数据
		String regex = "." ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true
public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "a\t" ;	// 要判断的数据
		String regex = "\\D\\s" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "" ;	// 要判断的数据
		String regex = "\\w*" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "ax" ;	// 要判断的数据
		String regex = "\\w{3,}" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//false

下面通过一些具体的范例来对正则的使用进行说明。

4:实现字符串替换(删除掉非字母与字母)

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "JILO&*()@#$UISD&*(#$HUK34rwyhui*()@#$*()@#$" ;	// 要判断的数据
		String regex = "[^a-zA-Z0-9]+" ;	// 正则表达式
		System.out.println(str.replaceAll(regex, ""));
	}
}
//JILOUISDHUK34rwyhui

5:实现字符串的拆分

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "a1b22c333d4444e55555f666666g" ;	// 要判断的数据
		String regex = "\\d+" ;	// 正则表达式
		String result [] = str.split(regex) ;
		for (int x = 0 ; x < result.length ; x ++) {
			System.out.print(result[x] + "、");
		}
	}
}
//a、b、c、d、e、f、g、

6:判断一个数据是否为小数

在这里插入图片描述

在这里插入图片描述

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "100.1" ;	// 要判断的数据
		String regex = "\\d+(\\.\\d+)?" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true

7:判断一个字符是否由日期组成,如果是由日期组成则将其转为Date类型

import java.text.SimpleDateFormat;

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "1981-20-15" ;	// 要判断的数据
		String regex = "\\d{4}-\\d{2}-\\d{2}" ;	// 正则表达式
		if (str.matches(regex)) {
			System.out.println(new SimpleDateFormat("yyyy-MM-dd").parse(str));
		}
	}
}
//Sun Aug 15 00:00:00 CST 1982

需要注意的是,正则表达式无法对里面的内容进行判断,只能够对格式进行判断处理

8:判断给定的电话号码是否正确?

在这里插入图片描述

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "51283346" ;	// 要判断的数据
		String regex = "\\d{7,8}" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true
public class Test {
	public static void main(String[] args) throws Exception {
		String str = "01051283346" ;	// 要判断的数据
		String regex = "(\\d{3,4})?\\d{7,8}" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "(010)-51283346" ;	// 要判断的数据
		String regex = "((\\d{3,4})|(\\(\\d{3,4}\\)-))?\\d{7,8}" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}
//true

9:验证email格式

在这里插入图片描述

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "mldnjava888@mldn.cn" ;	// 要判断的数据
		String regex = "[a-zA-Z0-9]\\w+@\\w+\\.(cn|com|com.cn|net|gov)" ;	// 正则表达式
		System.out.println(str.matches(regex));
	}
}

在这里插入图片描述

10:java.util.regex开发包

虽然大部分情况下都可以利用String类实现正则的操作,但是也有一些情况下需要使用java.util.regex开发包中提供的正则处理类,在这个包里面一共定义有两个类:Pattern(正则表达式编译),Match(匹配)。

在这里插入图片描述

import java.util.regex.Pattern;

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "JKL()UI$()QR@#JKLSD()QW#EIO$RJKLOSDF" ;
		String regex = "[^a-zA-Z]+" ;
		Pattern pat = Pattern.compile(regex) ; // 编译正则表达式
		String result [] = pat.split(str) ; // 拆分
		for (int x = 0 ; x < result.length ; x ++) {
			System.out.println(result[x]);
		}
	}
}
//
JKL
UI
QR
JKLSD
QW
EIO
RJKLOSDF

在这里插入图片描述

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

public class Test {
	public static void main(String[] args) throws Exception {
		String str = "101" ;
		String regex = "\\d+" ;
		Pattern pat = Pattern.compile(regex) ; // 编译正则表达式
		Matcher mat = pat.matcher(str) ;
		System.out.println(mat.matches());
	}
}
//true

在这里插入图片描述

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

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		String str = "1KLKLKL()8908923892389123890JKLJKHJKL&*()&*()U" ;
		String regex = "\\D+" ;
		Pattern pat = Pattern.compile(regex) ; // 编译正则表达式
		Matcher mat = pat.matcher(str) ;
		System.out.println(mat.replaceAll(""));
	}
}

在这里插入图片描述

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

public class JavaAPIDemo {
	public static void main(String[] args) throws Exception {
		// 要求取出“#{内容}”标记中的所有内容
		String str = "INSERT INTO dept(deptno,dname,loc) VALUES (#{deptno},#{dname},#{loc})" ;
		String regex = "#\\{\\w+\\}" ;
		Pattern pat = Pattern.compile(regex) ; // 编译正则表达式
		Matcher mat = pat.matcher(str) ;
		while(mat.find()) {	// 是否有匹配成功的内容
			System.out.println(mat.group(0).replaceAll("#|\\{|\\}", ""));
		}
	}
}

总结:java.util.regex开发包,如果不是进行一些更为复杂的正则处理时很难使用到的,而String类所提供的功能只是于正则的基本操作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值