博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java: Regular Expressions
阅读量:5034 次
发布时间:2019-06-12

本文共 2310 字,大约阅读时间需要 7 分钟。

[]只匹配单个字符

[枚举若干字符]:范围为所列字符。E.g. [aeiou] == a,e,i,o,u都可匹配。

[起点-终点]:范围为给定区间。

E.g.

[A-Y] == 大写字母A~Y都可匹配;

[A-Za-z] == 所有大写字母和小写字母都可匹配;

[A-z] == 大写A到小写z之间所有值,包括[,|等符号。

 

()组合

|:操作符左右任取其一。E.g. Hi (John|Jane)==匹配Hi John或Hi Jane。

 

Predefined Character Classes

Character Matches Character Matches
\d any digit \D any nondigit

\w

any word character \W any nonword character
\s any white-space character \S any non-whitespace character

\\d == 1或以上任意长度的数字串;(加在\d前的第一个\用于取消第二个\的转义)

\\s == 1或以上任意长度的空格串;

… etc。

 

Quantifiers

Quantifier Matches
* Matches zero or more occurrences of the pattern.
+ Matches one or more occurrences of the pattern.
? Matches zero or one occurrences of the pattern.
{n} Matches exactly n occurrences.
{n,} Matches at least n occurrences.
{n,m} Matches between n and m (inclusive) occurrences.

 

*:操作符前面紧接的pattern出现任意多次,可匹配空串。E.g. A, AAA都可匹配A*。

+:操作符前面紧接的pattern出现1或以上任意多次,不能匹配空串。E.g. A, AAA都可匹配A+,空串不能。

All quantifiers are greedy – will match as many occurrences as possible as long as the match is still successful.

But an ? following the quantifier can make it lazy  (reluctant) -- it then will match as few occurrences as possible.

 

e.g.

1 public class ValidateInput { 2      3     public static boolean validateFirstName(String firstName) { 4         return firstName.matches("[A-Z][a-zA-Z]*"); 5     }// Jane 6      7     public static boolean validateLastName(String lastName) { 8         return lastName.matches("[a-zA-z]+(['-][a-zA-Z]+)*"); 9     }// Doe10     11     public static boolean validateAddress(String address) {12         return address.matches("\\d+\\s+([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");13     } //10 Boradyway or 10 Main Street14     15     public static boolean validateCity(String city) {16         return city.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");17     } //Waltham or West Newton18     19     public static boolean validateState(String state) {20         return state.matches("([a-zA-Z]+|[a-zA-Z]+\\s[a-zA-Z]+)");21     } //SS22     23     public static boolean validateZip(String zip) {24         return zip.matches("\\d{5}");25     } //1234526     27     public static boolean validatePhone(String phone) {28         return phone.matches("[1-9]\\d{2}-[1-9]\\d{2}-\\d{4}");29     } //123-456-789030 31 }

 

转载于:https://www.cnblogs.com/RDaneelOlivaw/p/11511354.html

你可能感兴趣的文章
bzoj2120 数颜色
查看>>
Qt学习笔记网络(一)
查看>>
[个人翻译]Redis 集群教程(下)
查看>>
理清字符集和字符编码关系
查看>>
【转自官网】INS-30508 Invalid ASM Disks on Grid Infrastructure Installation (文档 ID 1999903.1)...
查看>>
Ubuntu下mysql修改连接超时wait_timeout
查看>>
日期格式化后转换为24时制
查看>>
如果在docker中部署tomcat,并且部署java应用程序
查看>>
匿名类型
查看>>
第四次作业
查看>>
函数的封装
查看>>
【转】Thread Local的正确原理与适用场景
查看>>
linux上mysql访问:Access denied for user 'agtipay'@'iZm5ebiyb4f90ga9xiycgsZ' (using password: YES)...
查看>>
idea下修改maven的setting.xml配置阿里云镜像
查看>>
5.泡妞与设计模式(六)创建者模式
查看>>
Linux centos7编译源码安装redis
查看>>
分割平面和空间的相关公式
查看>>
调试ASP.NET程序
查看>>
第三周学习进度
查看>>
access 清空后,自动编号怎么才能从0开始
查看>>