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

正则表达式

发布时间:2020-12-13 19:48:18 所属栏目:百科 来源:网络整理
导读:正则表达式: 正则表达式对字符串的常见操作 1. 匹配 其实使用的就是 String 类中的 matches 方法 Stringtel = "1232131212"; Stringregex = "1[358][0-9]{9}"//"1[358]d{9}" booleanb = tel.matches(regex); 2. 切割 其实使用的就是 String 类中的 split(

正则表达式:

正则表达式对字符串的常见操作

1.匹配

其实使用的就是String类中的matches方法

Stringtel = "1232131212";

Stringregex = "1[358][0-9]{9}"//"1[358]d{9}"

booleanb = tel.matches(regex);

2.切割

其实使用的就是String类中的split()方法

Stringstr = "ads sd s sd";

String[]name = str.split(" +");用一个或者多个空格进行切割

"."第一个转义第二个第二个转义.

"(.)1+" zhangtttkkad

使用()来表示组,表示第一组是任意字符,然后第二位和第一组相同

并且第二位使用转义字符,然后最少一次

3.替换

其实使用的就是String类中的replaceAll()方法

Stringstr = "adsdfsddddasadfd";

str= str.replaceAll("(.)1+","$1");

使用$符号来表示第二个参数使用的是第一个正则表达式中的第一组

Stringstr = "1230000123";

str= str.replaceAll("(d{3})d{4}(d{3})","$1****$2");

第一组和第二组保持一致,中间替换

4.获取

将正则规则进行对象的封装

Patternp = Pattern.compile("a*b");

通过正则对象的matcher方法和字符串关联,获取要对字符串操作的匹配对象Matcher

Matcherm = p.matcher("aaaab");

通过Matcher匹配器对象的方法对字符串进行操作

booleanb = m.matches();

获取三个字母的单词

Stringstr = "duan wujie bu fang jia!";

Stringregex = "b[a-z]{3}b" b是字符边界

将正则封装成对象

Patternp = Pattern.compile(regex);

通过正则对象获取匹配对象

Matcherm = p.matcher(str);

while(m.find()){

syso(m.group())//拿到单词

syso(m.start()+""+m.end());//该单词字母所在字符串的位置

}

应用:

治疗口吃:

1.我我....我要要要...要要学学学..学习习...习习习编编编.....编编编程程程....程程

publicstatic void test_1(){

Stringstr = "我我....我要要要...要要学学学..学习";

1.去掉字符串中.

str= str.replaceAll(".+","");

2.替换叠词

str= str.replaceAll("(.)1+","$1");

syso(str);

}

2.ip地址排序

Stringstr_ip = "192.168.1.1 1.1.1.133.4.5.6";

str_ip= str_ip.replaceAll("(d+)","00$1");

str_ip= str_ip.replaceAll("0*(d{3})","$1");

String[]ips = str_ip.split(" +");

TreeSet<String>ts = new TreeSet<String>();

for(Stingip:ips){

ts.add(ip);

}

for(Stingip:ts){

syso(ip.replaceAll("0*(d+)","$1"));

}

3.对邮件地址校验

Stringregex = "w+@w+(.w+)+";

网页爬虫

privatestatic List<String> getMailsByWeb() throws IOException {

//定义统一资源定位符对象

URLurl = new URL("http://lib.sdut.edu.cn/index.html");

BufferedReaderbufr = new BufferedReader(new InputStreamReader(url.openStream()));

//定义一个规则

Stringregex = "w+@w+(.w+)+";

//获取符合规则的字符

//创建模式规则对象

Patternp = Pattern.compile(regex);

Stringline = null;

List<String>list = new ArrayList<String>();

while((line= bufr.readLine())!=null){

//创建匹配对象

Matchermatcher = p.matcher(line);

while(matcher.find()){

list.add(matcher.group());

}

}

returnlist;

}

(编辑:李大同)

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

    推荐文章
      热点阅读