小标
2019-01-09
来源 :
阅读 1552
评论 0
摘要:本文主要向大家介绍了JAVA语言预定义字符类正则篇,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言预定义字符类正则篇,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
前面一篇了解了字符类,这篇来学习预定义字符类。没什么好说的,先每个字符类都写一个正则表达式来测试下,通过举例一些正向和反向的例子来练习。反正一开始,用得少记不住每个字符的含义,后面用多了,自然就会写正则表达式。
1. 在API文档关于预定义字符类

上面同样是用来匹配单个字符的
2. 代码练习
针对没个字符类写一个方法来测试
package regex;
public class Demo1_Regex {
public static void main(String[] args) {
//demo1();
//demo2();
//demo3();
//demo4();
//demo5();
//demo6();
demo7();
}
public static void demo7() {
String regex = "\\W"; // 表示非单词字符,就是标点符号
System.out.println("\n".matches(regex));
System.out.println("&".matches(regex));
System.out.println("#".matches(regex));
System.out.println("a".matches(regex));
System.out.println("9".matches(regex));
}
public static void demo6() {
String regex = "\\w"; // 表示单词字符:[a-zA-Z_0-9]
System.out.println("\n".matches(regex));
System.out.println("a".matches(regex));
System.out.println("H".matches(regex));
System.out.println("i".matches(regex));
System.out.println("Z".matches(regex));
System.out.println("9".matches(regex));
}
public static void demo5() {
String regex = "\\S"; // 表示非空白字符
System.out.println("\t".matches(regex));
System.out.println("a".matches(regex));
}
public static void demo4() {
String regex = "\\s"; // 表示空白字符
System.out.println("\t".matches(regex)); // 制表符
System.out.println("\f".matches(regex));
System.out.println("\n".matches(regex)); //回车并换行
System.out.println("\r".matches(regex)); // 回车符
}
public static void demo3() {
String regex = "\\D"; // 表示非数字,注意单个\表示转译字符
System.out.println("a".matches(regex));
System.out.println("你".matches(regex));
System.out.println("&".matches(regex));
System.out.println("7".matches(regex));
}
public static void demo2() {
String regex = "\\d"; // 表示数字0-9,注意单个\表示转译字符,所以这里需要\\d
System.out.println("0".matches(regex));
System.out.println("9".matches(regex));
System.out.println("10".matches(regex));
}
public static void demo1() {
String regex = ".";
System.out.println("a".matches(regex));
System.out.println("a".matches(regex));
System.out.println("ab".matches(regex));
String regex2 = "..";
System.out.println("ab".matches(regex2));
}
}
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号