正则表达式
来源:互联网 发布:sql点击安装没反应 编辑:程序博客网 时间:2024/04/30 04:07
正则表达式:
正则表达式对字符串的常见操作
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.1 33.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;
}
- 【正则表达式】正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 正则表达式
- 软工课题——实验报告二
- 多线程中对全局变量(整型类型)是否需要同步操作
- Lucene教程详解
- ios调用闪光灯
- protobuf是啥玩意儿
- 正则表达式
- Linux中如何让进程在后台运行
- 总结MySQL建表、查询优化实用小技巧
- csharp:汉字转带拼音声调
- New Problem 解题报告
- msql && sql
- 递归和非递归俩种方法实现二叉树的前序、中序、后续遍历
- csharp:汉字转带拼音声调
- dw内的dropdown dw列获取数据