java正则表达式

来源:互联网 发布:网络成瘾临床诊断标准 编辑:程序博客网 时间:2024/05/15 02:47

这里只是用例子来介绍说明....

01-正则表达式(概述)                                                                                                                                                                     

/* * 正则表达式。 * 正则表达式用于操作字符串数据。 * 通过一些特定的符号来体现的。 * 所以我们为了掌握正则表达式,必须要学习一些符号。 * */public class RegexDemo {public static void main(String[] args) {String qq="5234367";//checkQQ(qq);String regex="[1-9][0-9]{4-14}";//正则表达式。第一位数是1-9的数,第二位是0-9的数,后面的4-14位都是第二位区间相同boolean b=qq.matches(regex);System.out.println(qq+":"+b);}/*需求:定义一个功能对QQ号进行校验。 * 需求:长度是5~15.只能是数字,0不能开头 * */public static void checkQQ(String qq){int len=qq.length();if(len>=5&&len<=15){if(qq.startsWith("0")){if(!qq.startsWith("0")){try{long l=Long.parseLong(qq);System.out.println(l+":正确");}catch(NumberFormatException e){System.out.println(qq+"含有非法字符");}}}else{System.out.println(qq+"不能0开头"); }}else{System.out.println(qq+"长度错误");}}}

02-正则表达式(常见的规则)                                                                                                                                                     

/* * 正则表达式。 * 正则表达式用于操作字符串数据。 * 通过一些特定的符号来体现的。 * 所以我们为了掌握正则表达式,必须要学习一些符号。 * */public class RegexDemo {public static void main(String[] args) {String str="aoooooob";String reg="ao{4,6}b";boolean b=str.matches(reg);System.out.println(str+":"+b);}

03-正则表达式(常见的功能-匹配)                                                                                                                                            

public class RegexDemo2 {public static void main(String[] args) {/* * 正则表达式对字符串的常见的操作: * 1.匹配。 * 其实是用的就是String类中的matches方法。 * 2.切割。 * 其实使用的就是Strong类中的split方法。 * 3.替换。 *  * 4.获取。 *  * */functionDemo_1();}/* * 演示匹配。 * */public static void functionDemo_1(){//匹配手机号码是否正确。String tel="15800001111";String ter="158000a1111";String regex="1[358][0-9]{9}";boolean b=tel.matches(regex);boolean bo=ter.matches(regex);System.out.println(tel+":"+b);//15800001111:trueSystem.out.println(ter+":"+bo);//158000a1111:false}}

04-正则表达式(常见的功能-切割)                                                                                                                                            

/* * 切割 * 组:((A)(B(C))) * */public static void functionDemo_2(){String str="zhangsantttttxiaoqiangmmmmzhaoliu";String[]names=str.split("(.)\\1+");//str.split("\\. ");for(String name:names){System.out.println(name);}}zhangsanxiaoqiangzhaoliu
05-正则表达式(常见的功能-替换)                                                                                                                                            
public static void functionDemo_3() {String str="zhangsantttttxiaoqiangmmmmzhaoliu";str=str.replaceAll("(.)\\1+", "#");System.out.println(str);String tel="15800001111";//158****1111;tel=tel.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1*****$2" );System.out.println(tel);}

zhangsan#xiaoqiang#zhaoliu

158*****1111

06-正则表达式(常见的功能-获取)                                                                                                                                              

/* * 获取 * 将正则规则进行对象的封装 * Pattern p=Pattern.compile("a+b"); * 通过正则对象的matcher方法字符串关联。获取要对字符串操作的匹配器对象Matcher * Matcher m=p.matcher("aaaaab"); * 通过Matcher匹配器对象的方法对字符串进行操作。 * boolean b=m.matches(); * */public static void functionDemo_4() {String str="da jia hao,ming tian bu fang jia!";String regex="\\b[a-z]{3}\\b";//1,将正则封装成对象。Pattern p=Pattern.compile(regex);//2,通过正则对象获取匹配器对象。Matcher m=p.matcher(str);//使用Matcher对象的方法对字符串进行操作。//既然要获取三个字母组成的单词,查找:find();while(m.find()){System.out.println(m.group());//jia hao jia}//System.out.println(m.group());//获取匹配的子序列//jia}

0 0
原创粉丝点击