您的位置 首页 java

Java正则表达式

一、基础匹配

.:一个所有字符

d:一个所有数字

D:一个所有非数字

s:一个空白字符

S:一个非空白字符

w:一个[a-zA-Z_0-9]

W:一个非w

二、 ? 、*、+

X? X, once or not at all(0次或者1次)

X* X, zero or more times(0次或者多次)

X+ X, one or more times(1次或者多次)

三、范围匹配

X{n} X, exactly n times

X{n,} X, at least n times

X{n,m} X, at least n but not more than m times

[abc] a, b, or c

四、边界处理

(1)^不在[]中表示的是以XXX为开头,在[]中表示非XXX

(2)$表示以XXX结束

五、 Pattern 和Matcher

//可以先将一个正则表达式编译成Pattern对象,可以提高效率

Pattern p = Pattern.compile(“\d{4}”);

//可以通过Pattern对象获取一个Matcher对象,通过Matcher对象可以获取大量的信息

Matcher m = p.matcher(“1234”);

//判断是否匹配

System.out.println(m.matches());

//查找匹配的 字符串

System.out.println(m. find ());

//获取查找到的字符串

System.out.println(m.group());

六、分组与查找

(1)分组(数左括号)

(2)贪婪模式与非贪婪模式(后者多了一个?)

文章来源:智云一二三科技

文章标题:Java正则表达式

文章地址:https://www.zhihuclub.com/189642.shtml

关于作者: 智云科技

热门文章

网站地图