1 概述
正则表达式:正则表达式用于操作字符串数据。通过一些特定的符号来体现的。所以我们为了掌握正则表达式,必须要学习一些符号。虽然简化了,但是阅读性差。
需求:定义一个功能对QQ号进行校验。
要求:长度5~15. 只能是数字, 0不能开头
不用正则表达式的做法:
public class RegexDemo {
public static void main(String[] args) {
String qq = "123k4567";
checkQQ(qq);
}
public static void checkQQ(String qq){
int len = qq.length();
if(len>=5 && len<=15){
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+":长度错误");
}
}
}
正则表达式的做法:
public class RegexDemo {
public static void main(String[] args) {
String qq = "123k4567";
String regex = "[1-9][0-9]{4,14}";//正则表达式。
boolean b = qq.matches(regex);//String类中的方法,接受的必须是具备某种规则的字符串就是正则表达式,如果qq这个字符串与regex这个正则表达式匹配返回true
System.out.println(qq+":"+b);
}
}
2 正则表达式对字符串的常见操作-获取
将正则规则进行对象的封装。Pattern p = Pattern.compile("a*b");
通过正则对象的matcher方法字符串相关联。获取要对字符串操作的匹配器对象Matcher .Matcher m = p.matcher("aaaaab");
通过Matcher匹配器对象的方法对字符串进行操作。boolean b = m.matches();
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class RegexDemo {
public static void main(String[] args) {
functionDemo();
}
public static void functionDemo() {
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();
System.out.println(str);
while(m.find()){
System.out.println(m.group());//获取匹配的子序列
System.out.println(m.start()+":"+m.end());
}
}
3 正则表达式对字符串的常见操作-切割
其实使用的就是String类中的split方法。
public class Test {
public static void main(String[] args) {
functionDemo();
}
public static void functionDemo() {
String str = "zhangsan xiao zhaoliu";
String[] names = str.split(" ");//用空格来切时候,如果有很多空格,切出来的效果就不好了
for(String name : names){
System.out.println(name);
}
}
}
public class Test {
public static void main(String[] args) {
functionDemo();
}
public static void functionDemo() {
String str = "zhangsan xiaoqiang zhaoliu";
String[] names = str.split(" +");//表示多个空格
for(String name : names){
System.out.println(name);
}
}
}
public class Test {
public static void main(String[] args) {
functionDemo();
}
public static void functionDemo() {
String str = "zhangsan.xiaoqiang.zhaoliu";
String[] names = str.split("\\.");
for(String name : names){
System.out.println(name);
}
}
}
组:((A)(B(C)))
public class Test {
public static void main(String[] args) {
functionDemo();
}
public static void functionDemo() {
String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";
String[] names = str.split("(.)\\1+");//加上括号进行封装,有编号
for(String name : names){
System.out.println(name);
}
}
}
4 正则表达式对字符串的常见操作-替换
其实使用的就是String类中的replaceAll()方法。
public class RegexDemo {
public static void main(String[] args) {
functionDemo();
}
public static void functionDemo() {
String str = "zhangsanttttxiaoqiangmmmmmmzhaoliu";
str = str.replaceAll("(.)\\1+", "$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);
}
}
5 正则表达式对字符串的常见操作_匹配
其实使用的就是String类中的matches方法。
public class RegexDemo {
public static void main(String[] args) {
functionDemo();
}
//演示匹配:匹配手机号码是否正确。
public static void functionDemo(){
String tel = "15800001111";
//String regex = "1[358][0-9]{9}";
String regex = "1[358]\\d{9}";//用到反斜线的时候要用到两个,因为只用一个的话会把d给转义了
boolean b = tel.matches(regex);
System.out.println(tel+":"+b);
}
}
6 练习
1.对ip地址排序:192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55
public class RegexTest {
public static void main(String[] args) {
test();
}
public static void test(){
String ip_str = "192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55 225.234.234.255";
//1,为了让ip可以按照字符串顺序比较,只要让ip的每一段的位数相同。
//所以,补零,按照每一位所需做多0进行补充。每一段都加两个0.
ip_str = ip_str.replaceAll("(\\d+)", "00$1");
System.out.println(ip_str);
//然后每一段保留数字3位。
ip_str = ip_str.replaceAll("0*(\\d{3})", "$1");
System.out.println(ip_str);
//将ip地址切出。
String[] ips = ip_str.split(" +");
TreeSet<String> ts = new TreeSet<String>();
for(String ip : ips){
// System.out.println(ip);
ts.add(ip);
}
for(String ip : ts){
System.out.println(ip.replaceAll("0*(\\d+)", "$1"));
}
}
}
2.对邮件地址校验
public class RegexTest {
public static void main(String[] args) {
test();
}
public static void test() {
String mail = "abc1@sina.com.cn";
String regex = "[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{1,3})+";
regex = "\\w+@\\w+(\\.\\w+)+";//1@1.1
boolean b = mail.matches(regex);
System.out.println(mail+":"+b);
}
}