JavaSE_52th_正则表达式

一、正则表达式
1、用途
1)字符串匹配(字符匹配)
2)字符串查找
3)字符串替换

2、例如
1)IP地址是否正确
2)从网页中揪出email地址
3)从网页中揪出链接等

3、类
1)java.lang.String
2)java.util.regex.Pattern
3)java.util.regex.Matcher

4、String类方法
1)matches
public boolean matches(String regex) //告知此字符串是否匹配给定的正则表达式。 
可以看出正则表达式本身也是字符串。
调用此方法的str.matches(regex)形式与以下表达式产生的结果完全相同: 
Pattern.matches(regex, str)

package com.hpe.regexp;

public class Test {
	public static void main(String[] args) {
		System.out.println("abc".matches("..."));
	}
}
运行结果:
false
总结:
①正则表达式中一个点代表一个字符。
②检查字符串是否匹配3个字符的格式。
③"abc"长度为3,并且都是字符,所以匹配。

二、replaceAll
public String replaceAll(String regex,
                         String replacement)
//使用给定的 replacement 替换此字符串所有匹配给定的正则表达式的子字符串。

package com.hpe.regexp;

public class Test2 {
	public static void main(String[] args) {
		print("abc".matches("..."));
		print("lk3728ew".replaceAll("\\d", "-"));
	}
	
	public static void print(Object o) {
		System.out.println(o);
	}
}
运行结果:
true
lk--------ew

总结:
①正则表达式中\d表示数字。
②Java中\是转义字符,显示转义字符本身需要在前面再加上转义字符。
③所以表示表示数字需要写成\\d。


5、Pattern类方法
public static Pattern compile(String regex)
compile()方法表示把正则表达式编译一下,然后放到Pattern对象中,Pattern是指字符串要被匹配的模式,然后调用这个Pattern对象的matcher()方法去匹配某一个字符串,匹配的结果会存放在Matcher对象中,Matcher对象调用matches方法,返回值表明了该字符串是否匹配正则表达式的格式。

以下是Pattern的matcher()方法,其返回值是一个Matcher对象,存放了匹配结果。其中CharSequence是String类的父接口。
public Matcher matcher(CharSequence input)

以下是Matcher的matches()方法,其返回值是boolean类型,表明匹配结果。
public boolean matches()

6、举例

1)例1

package com.hpe.regexp;

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

public class Test3 {
	public static void main(String[] args) {
		print("abc".matches("..."));
		print("lk3728ew".replaceAll("\\d", "-"));
		/* 匹配一个具有3个字符的字符串,这3个字符每个都是a-z范围内的字母 */
		Pattern p = Pattern.compile("[a-z]{3}");
		Matcher m = p.matcher("weq");
		print(m.matches());
	}
	
	public static void print(Object o) {
		System.out.println(o);
	}
}
运行结果:
true
lk----ew
true

2)例2
package com.hpe.regexp;

public class Test4 {
	public static void main(String[] args) {
		//初步认识 . * + ?
		/* 是否匹配一个字符 */
		print("a".matches("."));//true
		/* 是否匹配两个a */
		print("aa".matches("aa"));//true
		/* a*表示a出现0次或多次,即有n个字符就有n个a,(且n>=0) */
		print("aaaa".matches("a*"));//true
		/* a+表示a出现1次或多次,即有n个字符就有n个a,(且n>=1) */
		print("aaaa".matches("a+"));//true
		/* a?表示a出现0次或1次,即有n个字符就有n个a,(且n=0,1) */
		print("aaaa".matches("a?"));//false
		/* a出现0次 */
		print("".matches("a?"));//true
		/* a出现1次 */
		print("a".matches("a?"));//true
		/* 每一位都是数字,至少3个,不超过100个 */
		print("935265681237985131".matches("\\d{3,100}"));//true
		/* \.是正则表达式中的. 但是Java中\需要转义,因此还需要再加一个\,即\\.表示一个. */
		print("192.168.0.aaa".matches("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}"));//false
		/* 检查每一位数字的取值范围 */
		print("192".matches("[0-2][0-9][0-9]"));//true
	}
	
	public static void print(Object o) {
		System.out.println(o);
	}
}


3)例3
package com.hpe.regexp;

public class Test5 {
	public static void main(String[] args) {
		//范围
		/* 该字符a匹配abc中的一个 */
		print("a".matches("[abc]"));//true
		/* 该字符a匹配除了abc之外的字符 */
		print("a".matches("[^abc]"));//false
		/* 该字符A是从a-z范围或者A-Z范围,相当于取并集 */
		print("A".matches("[a-zA-z]"));//true
		/* 同上 */
		print("A".matches("[a-z]|[A-Z]"));//true
		/* 同上 */
		print("A".matches("[a-z[A-Z]]"));//true
		/* 该字符R是从A-Z范围中并且是RFG中的一个,相当于取交集 */
		print("R".matches("[A-Z&&[RFG]]"));//true
	}
	
	public static void print(Object o) {
		System.out.println(o);
	}
}


4)例4
package com.hpe.regexp;

public class Test6 {
	public static void main(String[] args) {
		//认识空白字符
		/* 有4个字符,并且每一位都是空白字符 */
		print(" \n\t\r".matches("\\s{4}"));//true
		/* 有1个字符,并且是非空白字符 */
		print(" ".matches("\\S"));//false
		/* 有3个字符,并且每一位都是word字符(字母、数字、下划线) */
		print("a_8".matches("\\w{3}"));//true
		/* 从左到右:1到3位的小写字母、至少出现1次的数字、 &^#%中的字符至少出现一次*/
		print("abc888&^%".matches("[a-z]{1,3}\\d+[&^#%]+"));//true
		/* 是否匹配\ */
		//print("\\".matches("\\"));//抛java.util.regex.PatternSyntaxException异常
		/* 正则表达式中\也是特殊字符,所以也要写成\\,这就要求在Java中要写成\\\\ */
		print("\\".matches("\\\\"));//true
	}
	
	public static void print(Object o) {
		System.out.println(o);
	}
}



5)例5
package com.hpe.regexp;

public class Test7 {
	public static void main(String[] args) {
		
		/* 以h开头,之后的字符可以是任意字符,出现0次或多次 */
		print("hello sir".matches("^h.*"));//true
		/* 以ir结尾,前面是出现0次或多次的任意字符 */
		print("hello sir".matches(".*ir$"));//true
		
		/* \b表示一个字符的边界 */
		/* 以h开头,小写字母出现1-3次,之后是一个字母o,
		 * 然后是word字符边界(如空白字符),再后面是0次或多次的任意字符 */
		print("hello sir".matches("^h[a-z]{1,3}o\\b.*"));//true
		/* 下面没有出现word字符边界 */
		print("hellosir".matches("^h[a-z]{1,3}o\\b.*"));//false
		
		//空白行,这里认为不以换行开头的、有特殊字符的行为空白行
		print(" \n".matches("[\\s&&[^\\n]]*\\n$"));//true
	}
	
	public static void print(Object o) {
		System.out.println(o);
	}
}





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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值