【Java学习笔记】正则表达式·集合和列表

本文详细讲解了Java中的正则表达式,包括校验手机号和邮箱、替换子字符串、切割字符串等操作。此外,还介绍了集合的基础知识,如数组与集合的区别、集合的特性、常用方法及面试题解析,特别关注了List接口及其迭代器的使用。
摘要由CSDN通过智能技术生成

.正则表达式

1.正则表达式的校验

1)典型顺序

Pattern p = Pattern.compile("a*b") ;

Matcher m = p.matcher("aaaaaaaab") ;

boolean flag = m.matches() ;

通过Pattern模式类中compile()要获取模式对象,通过里面的matcher()方法获取匹配器对象Matcher,再去使用

2)简化版

String regex = "a*b" ;

String s = "aaaab" ;

boolean flag = s.matches(regex) ;

2.正则校验手机号

String phone = sc.nextLine();

boolean flag = phone.matches(regex) ;

3.正则校验邮箱号

String email = sc.nextLine() ;

String regex = "\\w+@\\w{2,6}(\\.\\w{2,3})+" ;

boolean flag = email.matches(regex) ;

4.替换正则表达式的子字符串

String s = "hello1234567world6891234java" ;

String regex = "[0-9]+";

String regex = "\\d+" ;

String ss = "*" ;

String result = s.replaceAll(regex, ss) ;

5. 根据给定正则表达式的匹配切割此字符串

String s = "aa,bb,cc" ;

String[] strArray = s.split(",") ;

for(int x = 0 ; x < strArray.length ; x++){

System.out.println(strArray[x]);}

6.例题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值