码迷,mamicode.com
首页 > 其他好文 > 详细

正则表达式

时间:2018-12-27 03:18:00      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:string   表达式   等价   regexp   find   abc   span   关于   efi   

说明:正则在大多数程序语言中都是适用的,个人觉得学不好正则的程序员是个不合格的程序员,所以突然想整理一篇关于正则表达式的文章,以免用到的时候去百度。下面进入正题

一、使用正则表达式的目的

1.可以截取我们想要字符的部分

2.可以替换字符串的部分(文本全局替换)

3.检查字符串是否符合要求

二、正则元字符

  \  反斜杠转义字符  例如 \n 匹配换行符  \t匹配空格  \r 相当于回车

  ^ 行首

  $ 行尾

  * 表示前面子表达式任意次数,等价于{0,}

  + 表示前面子表达式次数大于等于1,等价于{1,}

  ?表示前面子表达式零次或一次 等价于{0,1}  例如“yes(no)?”  可以匹配 yes或者yesno

  {n} 表示前面子表达式n次

  {n,}至少匹配n次

  {n,m} 匹配次数在n,m之间保含n,m

  ?紧跟在限制符后面*,+,?,{n},{n,},{n,m}后面时,匹配模式为非贪婪模式,竟可能少的匹配,贪婪模式为

  尽可能多的匹配。例如“oooo” “o+” 匹配结果为["o","0","o","o"],而贪婪模式为["oooo"]

 1         String str1="oo123ooo";
 2         Pattern pattern1=Pattern.compile("o+");
 3         Pattern pattern2=Pattern.compile("o+?");
 4         Matcher matcher1 = pattern1.matcher(str1);
 5         while (matcher1.find()){
 6             System.out.println("m1:"+matcher1.group(0)); //m1:oo m1:ooo
 7         }
 8         Matcher matcher2 = pattern2.matcher(str1);
 9         while(matcher2.find()){
10             System.out.println("m2:"+matcher2.group(0));// m2:o m2:o m2:o m2:o m2:o
11         }

  x|y 匹配x或y 

  [xyz] 匹配xyz任一字符

  [^xyy] 匹配除xyz任一字符

  [a-z] 任一小写字母

  [A-Z] 任一大写字母

  [0-9] 0-9中任一数字  等价于 \d

  [^0-9] 除0-9意外任一字符  等价于\D

  \f 换页符

  \w 等价于[a-zA-Z0-9_]  匹配单词字符

  \W 匹配非单词字符

  \s 匹配任何不可见字符 等价于[\f\n\r\t\v]

  \S 匹配任何可见字符

 

三、java中实例应用

  1.替换

        String str3="abc345abc678";
        String regex3="[a-z]+";
        String str4 = str3.replaceAll(regex3, "hello");
        System.out.println(str4); //hello345hello678
        非懒惰模式
String str3="abc345abc678"; String regex3="[a-z]+?"; String str4 = str3.replaceAll(regex3, "hello"); System.out.println(str4); //hellohellohello345hellohellohello678

 

  2.判断是否符合正则

//判断qq邮箱

        String email="1019207623@qq.com";
        String regex4="^[1-9][0-9]{4,10}@qq.com$";
        System.out.println(email.matches(regex4));  //true

 

  3.Pattern对象 获取字符串中匹配正则的所有matcher集合

 1         String str1="oo123ooo";
 2         Pattern pattern1=Pattern.compile("o+");
 3         Pattern pattern2=Pattern.compile("o+?");
 4         Matcher matcher1 = pattern1.matcher(str1);
 5         while (matcher1.find()){
 6             System.out.println("m1:"+matcher1.group(0)); //m1:oo m1:ooo
 7         }
 8         Matcher matcher2 = pattern2.matcher(str1);
 9         while(matcher2.find()){
10             System.out.println("m2:"+matcher2.group(0));// m2:o m2:o m2:o m2:o m2:o
11         }

四、js中实例应用

  1.判断合法url

  

var r=new RegExp(/^(http)s?:\/\/\w+.\w+.com$/)
undefined
r.test(‘http://www.baidu.com‘)
true

  2.js替换   跟上g可以全局替换

var r=/http/g
undefined
‘http://www.baidu.com.http‘.replace(r,‘https‘)
"https://www.baidu.com.https"

 

 

 

  

正则表达式

标签:string   表达式   等价   regexp   find   abc   span   关于   efi   

原文地址:https://www.cnblogs.com/wjw0324/p/10180632.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!