在安卓中使用正则表达式1

Section1  Pattern

首先理解这个单词:Pattern

Pattern 是什么意思?

中文译为模式,在深度学习领域,有所谓的模式识别的概念

手机号是一种模式,邮箱是也是一种模式,网址又是另外一直模式



Section2   ^和$ 使用

假设我想判断一个字符串是否以The开头要怎么做

System.out.println(Pattern.matches("^The.*", "The gril") + "");

以The结尾的呢?

System.out.println(Pattern.matches(".*The$", "gril The") + "");

^代表开头,$代表结尾


Section3  * , + , ?


先看*  (0个或多个)

System.out.println(Pattern.matches("^ab*", "a") + "");
System.out.println(Pattern.matches("^ab*", "ab") + "");
System.out.println(Pattern.matches("^ab*", "abb") + "");
true
true
true


再看+(1个或更多)

System.out.println(Pattern.matches("^ab+", "a") + "");
System.out.println(Pattern.matches("^ab+", "ab") + "");
System.out.println(Pattern.matches("^ab+", "abb") + "");
false
true
true


然后看?(零个或一个)

System.out.println(Pattern.matches("^ab?", "a") + "");
System.out.println(Pattern.matches("^ab?", "ab") + "");
System.out.println(Pattern.matches("^ab?", "abb") + "");
true
true
false

Section4  {} 表示次数
ab{2}  ==》a后面两个b
ab{2,}  ==》a后面两个或更多个b
ab{3,5}  ==>a后面3到5个b
==========================================
其实  * ===》{0,}
      + ===》{1,}
      ? ===》{0,1}


Section5  | 

| 逻辑或

(1)  ab|ba  ===>ab或ba


(2)(ab|ba)cd  ===>abcd 或bacd

System.out.println(Pattern.matches("^(ab|ba)cd", "abcd") + "");

System.out.println(Pattern.matches("^(ab|ba)cd", "bacd") + "");
true
true


(a|b)*c   ===> ab混合后面来个c

System.out.println(Pattern.matches("^(a|b)*c", "abc") + "");
System.out.println(Pattern.matches("^(a|b)*c", "abbbaac") + "");
System.out.println(Pattern.matches("^(a|b)*c", "baabbaac") + "");









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值