正则表达式总结(内含测试)

一、限定符

符号说明
*匹配前面的符号或者子表达式零次或多次
+匹配前面的符号或者子表达式一次或多次
?匹配前面的符号或者子表达式零次或一次
{n}匹配前面的符号或者子表达式n次
{n,}匹配前面的符号或者子表达式n次及以上
{n,m}匹配前面的符号或者子表达式n次到m次

测试如下:

		// *
        String str = "abcabc";
        System.out.println(str.matches(".*"));//true
        System.out.println("".matches(".*"));//true
        // +
        System.out.println(str.matches(".+"));//true
        System.out.println("".matches(".+"));//false
        // ?
        System.out.println(str.matches(".?"));//false
        System.out.println("".matches(".?"));//true
        // {n}
        System.out.println(str.matches(".{2}"));//false
        System.out.println(str.matches(".{6}"));//true
        // {n,}
        System.out.println(str.matches(".{2,}"));//true
        System.out.println(str.matches(".{3,}"));//true
        // {n,m}
        System.out.println(str.matches(".{1,3}"));//false
        System.out.println(str.matches(".{3,6}"));//true

二、非打印字符

字符说明
\cx\cx中x是控制字符(即control+键盘上某个字符(A-Za-z这些字符)),比如:\cJ就等价与control+J(即\n) ,使用时\cx
\f换页符,等价于\x0c和\cL
\n换行符,等价于\x0a和\cJ
\r回车符,等价于\x0d和\cM
\t制表符,等价于\0x9和\cI
\s匹配任何空白内容,包括空格和[\r\n\f\t]有些地方会有\v(垂直制表符)Unicode编码包括全角空格
\S刚好与\s相反,匹配任何非空白内容

以上除了部分转义字符,其他的在具体使用的时候都是双反斜杠\\
测试如下:

        // \cx
        System.out.println("\r".matches("\\cM"));//true
        System.out.println("\t".matches("\\cI"));//true
        System.out.println("\n".matches("\\cJ"));//true
        System.out.println("\f".matches("\\cL"));//true
        // \f \n \r \t 与十六进制字符的正则表达式
        System.out.println("\f".matches("\\x0c"));//true
        System.out.println("\n".matches("\\x0a"));//true
        System.out.println("\r".matches("\\x0d"));//true
        System.out.println("\t".matches("\\x09"));//true
        //\s \S
        System.out.println("\n\t\f\r".matches("\\s{4}"));//true
        System.out.println("asdf".matches("\\S{4}"));//true

三、特殊字符

符号说明
^匹配输入字符的开始位置;当其在方括号([ ])中时表示不接受方括号表达式中的字符集合
$匹配输入字符的结束位置;当设置了RegExp对象的Multiline属性则$也可以匹配\n和\r
[标记一个方括号表达式的开始
]标记一个方括号表达式的结束
(标记一个子字符串表达式的开始
(标记一个子字符串表达式的结束
{标记限定表达式的开始
}标记限定表达式的结束
|指明两项之间的选择
\将一个字符标记为转义字符或者原义字符

测试如下:

 		System.out.println("132133513".matches("^\\d+$"));//true
        System.out.println("safasf".matches("[a-z]+"));//true
        System.out.println("sagsdgs".matches("\\w+(@163\\.com)?"));//true
        System.out.println("sagsdgs@163.com".matches("\\w+(@163\\.com)?"));//true
        System.out.println("18312345645".matches("(183){1}\\d+"));//true
        System.out.println("0.123".matches("([1-9]\\d*|0)\\.\\d*"));//true
        System.out.println("101.123".matches("([1-9]\\d*|0)\\.\\d*"));//true
        System.out.println(".".matches("\\."));//true

最后介绍几种常用的正则表达式:

字符说明
\w全部普通字符,即[a-zA-z0-9_]
\W特殊字符(不包含下划线),即[~!@#$%^&*()_=+;:’。等]
\d全部数字,即[0-9]
.除‘\n’换行符外其它任意单字符

测试如下:


        System.out.println("sadasd".matches("\\w*"));//true
        System.out.println("asdsa56f46as64@@$".matches(".*"));//true
        System.out.println("061568415".matches("\\d*"));//true
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Regex Testor Version 1.02 Copyright (c) 2013 Fiery Red - flameleo 我们在使用正则表达式( regex: regular expression )的过程中,经常发现正则表达式的语法很令人头疼,即使对经常使用它的人来说也是如此。对于刚接触正则表达式的人来说多练习,多使用,才能熟练掌握正则表达式。 由于难于读写,容易出错,且需要反复练习。所以找一种工具对正则表达式对我们脑中构思的regex进行测试是很有必要的。 1.特点 a.适合初学者,在不断测试用学习如何使用正则表达式。 b.可以分组保存,测试中用到的正则表达式。 c.本工具使用最常见的regex。以下是简单示例: a|b Matches a or b gr(a|e)y Matches a or e . Matches any single character [abc] Matches a single character a, b or c [^abc] Matches any single character except a, b or c [a-z] Matches a single charactor in the range a to z [a-zA-Z] Matches a single charactor in the range a to z or A to Z ^ Matches the start of the filename $ Matches the end of the filename * Matches the preceding element zero or more times ? Matches the preceding element zero or one times + Matches the preceding element one or more times {x} Matches the preceding element x times {x,} Matches the preceding element x or more times {x,y} Matches the preceding element between x and y times 值得一提的是()代表子匹配,有些环境中gr(a|e)y Matches gray or grey 还支持许多常见的转义字符 \b,\B,\c,\d,\D,\f,\n,\r,\s,\S,\t,\v,\w,\W,\x,\u 具体详见附件 Regular Expression Syntax1.html 2.功能介绍 a.界面上显示提供regex输入框和原文本框,点击[模式匹配]按钮后,会在右侧输出结果,包括匹配字符串列表和文本。 b.对于测试中一些有用的regex,点击[insert]按钮添加到模式列表,以备日后使用。你可以位该regex添加描述分组,该信息会在程序结束后保存在CustomPatternInfo.ini文件中。 c.可以参考Readme_1.jpq和Readme_2.jpq图片介绍。 3.有待改进 a.界面布局和控件友好型和交互性。 b.界面功能提供regex语法支持。(暂时可以通过导入附件RegexSystax.ini到CustomPatternInfo.ini中) 4.意见反馈 a.请将您的宝贵意见反馈到 FieryRed_2012@163.com 附件: Readme.txt Readme_1.jpg Readme_2.jpg Regular Expression Syntax.html RegexSystax.ini

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值