吾爱乐享
个人学习网站

java之学习正则预定义字符类的用法

结果示意图

预定义字符类

. 任何字符(与行结束符可能匹配也可能不匹配)
\d 数字:[0-9]
\D 非数字: [^0-9]
\s 空白字符:[ \t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]

案例代码

 

package com.ifenx8.regex;

public class Demo3_Regex {

	/**
	 * 预定义字符类 
		. 任何字符(与行结束符可能匹配也可能不匹配) 
		\d 数字:[0-9] 
		\D 非数字: [^0-9] 
		\s 空白字符:[ \t\n\x0B\f\r] 
		\S 非空白字符:[^\s] 
		\w 单词字符:[a-zA-Z_0-9] 
		\W 非单词字符:[^\w] 

	 */
	public static void main(String[] args) {
		demo1();//  . 任何字符(与行结束符可能匹配也可能不匹配) 
		demo2();//  \d 数字:[0-9] 
		demo3();//  \D 非数字: [^0-9]
		demo4();//  \s 空白字符:[ \t\n\x0B\f\r] 
		demo5();//	\S 非空白字符:[^\s]
		demo6();//	\w 单词字符:[a-zA-Z_0-9]
		demo7();//	\W 非单词字符:[^\w]

	}

	public static void demo7() {
		String regex = "\\W";//全部的非单词字符(除了a-z  A-Z  0-9  还有_下划线)
		System.out.println("a".matches(regex));//false
		System.out.println("1".matches(regex));//false
		System.out.println("A".matches(regex));//false
		System.out.println("_".matches(regex));//false
		System.out.println("#".matches(regex));//true
	}

	public static void demo6() {
		String regex = "\\w";//全部的单词字符包括a-z  A-Z  0-9  还有_下划线
		System.out.println("a".matches(regex));//true
		System.out.println("1".matches(regex));//true
		System.out.println("A".matches(regex));//true
		System.out.println("_".matches(regex));//true
		System.out.println("#".matches(regex));//false
		System.out.println("============");
	}

	public static void demo5() {
		String regex = "\\S";//所有的非空白字符
		System.out.println("	".matches(regex));//tab键 返回false
		System.out.println(" ".matches(regex));//空格键 返回false
		System.out.println("    ".matches(regex));//四个空格键长度和一个tab键一样但是他是四个字符,返回false
		System.out.println("2".matches(regex));//其他任意非空白字符 返回true
		System.out.println("==============");
	}

	public static void demo4() {
		String regex = "\\s";//空格 tab键 回车  换行等返回true
		System.out.println("	".matches(regex));//tab键
		System.out.println(" ".matches(regex));//空格键
		System.out.println("    ".matches(regex));//四个空格键长度和一个tab键一样但是他是四个字符,返回false
		System.out.println("==============");
	}

	public static void demo3() {
		String regex = "\\D";//表示除了数字0-9以外的全部字符
		System.out.println("1".matches(regex));//false
		System.out.println("a".matches(regex));//true
		System.out.println("0".matches(regex));//false
		System.out.println("#".matches(regex));//true
		System.out.println("===============");
	}

	public static void demo2() {
		String regex = "\\d";// 代表0-9的数字,因为一个\是表示转义字符,所以要在\d前面加个\
		System.out.println("1".matches(regex));//true
		System.out.println("0".matches(regex));//true
		System.out.println("a".matches(regex));//false
		System.out.println("#".matches(regex));//false
		System.out.println("===============");
	}

	public static void demo1() {
		String regex = ".";//匹配任意字符
		System.out.println("a".matches(regex));//true
		System.out.println("#".matches(regex));//true
		System.out.println("1".matches(regex));//true
		System.out.println("ab".matches(regex));//false  一个“.”表示一个字符 ab为两个字符 所以返回false
		System.out.println("===========");
	}

}

赞(0) 打赏
未经允许不得转载:吾爱乐享 » java之学习正则预定义字符类的用法

评论 抢沙发

评论前必须登录!

 

推荐免费资源共享,个人经验总结学习

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏