Java学习提要——String类对正则的支持

先来看看String类对正则的操作方法

方法名称类型描述
public boolean matches(String regex)普通正则验证,使用指定字符串判断其是否符合给出的正则表达式结构
public String replaceAll(String regex,String replacement)普通全部替换
public String replaceFirst(String regex,String replacement)普通替换首个
public String[] split(String regex)普通全部拆分
public String[] split(String regex,int limit)普通部分拆分

例:

//输出小写字母
public class Nice {
    public static void main(String args[]) {
        String str = "sdfAFUsfi[sf*sfSDwr*sYD" ;
        String regex = "[^a-z]" ;  //此处编写正则
        System.out.println(str.replaceAll(regex,"")) ;
    }
}
//用数字拆分
public class Nice {
    public static void main(String args[]) {
        String str = "sdf34AFU67sf789i[sf*s90-=fSDw345r*sYD" ;
        String regex = "\\d+" ;  //[0-9]此处编写正则
        String result[] = str.split(regex) ;
        for (int x=0 ; x < result.length ; x++) {
            System.out.println(result[x]) ;
        }
    }
}
//验证字符串是否是数字,如果是则变为double型
public class Nice {
    public static void main(String args[]) {
        String str = "100.01" ;
        String regex = "\\d+(\\.\\d+)?" ;
        System.out.println(str.matches(regex)) ;
        if (str.matches(regex)) {  //转型之前进行验证
            System.out.println(Double.parseDouble(str)) ;
        }
    }
}
//判断ipv4
public class Nice {
    public static void main(String args[]) {
        String str = "192.168.1.1" ;
        String regex = "(\\d{1,3}\\.){3}\\d{1,3}" ;  //由于有重复,所以进行了简化
        System.out.println(str.matches(regex)) ;
    }
}
//判断是否为日期格式,如果是则转换为Date型数据
import java.text.SimpleDateFormat ;
import java.util.Date ;
public class Nice {
    public static void main(String args[]) throws Exception{
        String str = "2111-11-11" ;  //无法判断日期的有效性
        String regex = "\\d{4}-\\d{2}-\\d{2}" ;
        System.out.println(str.matches(regex)) ;
        if (str.matches(regex)) {
            Date date = new SimpleDateFormat("yyyy-MM-dd").parse(str);
            System.out.println(date) ;
        }
    }
}
//输出电话号码
//(010)-12345678或者010-12345678或者12345678
import java.text.SimpleDateFormat ;
import java.util.Date ;
public class Nice {
    public static void main(String args[]) throws Exception{
        String str = "(010)-12345678" ;
        String regex = "((\\d{3,4}-)|(\\(\\d{3,4}\\)-))?\\d{7,8}" ;  //为了解决括号的问题,才会有那么多转义
        System.out.println(str.matches(regex)) ;
    }
}
//email的判断,给@前面设置字数范围与结尾字符范围,后缀设置限制
public class Nice {
    public static void main(String args[]) throws Exception{
        String str = "nihao_Nice123@nice.com.cn" ;
        String regex = "[a-zA-Z][a-zA-Z0-9_\\.]{0,28}[a-zA-Z0-9]@\\w+\\.(net|cn|com\\.cn|net\\.cn|org|gov|edu)" ;
        System.out.println(str.matches(regex)) ;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值