正则表达式
正则表达式:正则表达式对字符串的常见操作 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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |