正则表达式

正则表达式主要在java.util.regex包中,有Pattern和Matcher类。

Pattern类主要是正则匹配规则,Matcher是用某个正则表达式去匹配字符串。

正则表达式

正则表达式

Pattern和Matcher提供的常用方法:


代码示例:

(1)匹配日期格式yyyy-MM-dd格式:

(2)匹配5个以上的数字:

(3)匹配一个以上数字:

(4)用“_”替换数字

(5)以数字为分割:



字符串中也提供了正则表达式的方法:

(1)matches(String regex);

(2)replaceAll(String regex,String replace);

(3)split(String regex);






你可能感兴趣的:(正则表达式)