前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java之学习正则预定义字符类的用法

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

作者头像
吾爱乐享
发布2018-07-13 12:01:16
4380
发布2018-07-13 12:01:16
举报
文章被收录于专栏:吾爱乐享

结果示意图

预定义字符类

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

案例代码

代码语言:javascript
复制
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("===========");
	}

}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017-08-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 结果示意图
  • 预定义字符类
  • 案例代码
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档