加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式的学习一

发布时间:2020-12-14 01:21:55 所属栏目:百科 来源:网络整理
导读:一、matches方法 matches方法可以判断当前的字符串是否匹配给定的正则表达式。如果匹配,返回true,否则,返回false。 matches方法的定义如下: span style="font-size:18px;"public boolean matches(String regex)/span 二、split方法 split方法使用正则表

一、matches方法

matches方法可以判断当前的字符串是否匹配给定的正则表达式。如果匹配,返回true,否则,返回false。

matches方法的定义如下:

<span style="font-size:18px;">public boolean matches(String regex)</span>

二、split方法

split方法使用正则表达式来分割字符串,并以String数组的形式返回分割结果。split有两种重载形式,它们定义如下:

public String[] split(String regex)
public String[] split(String regex,int limit)
三、replaceAll 和 replaceFirst方法

为两个方法的定义如下:
public String replaceAll(String regex,String replacement)
public String replaceFirst(String regex,String replacement)
 
这两个方法用replacement替换当前字符串中和regex匹配的字符串。

四、Pattern、Matcher的使用

Pattern pattern = Pattern.compile("[a-z]{3}");
Matcher m = pattern.matcher("asd");
m.matches();
Pattern对正则表达式进行编译,Matcher使用编译后的正则去匹配字符串。

这种“预编译”的方法可以提高运行效率。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读