黑马程序员-正则表达式
2014-01-15 23:40
567 查看
------- android培训、java培训、期待与您交流! ----------
正则表达式的特点:
正则表达式用于操作字符串数据
通过一些特定的符号来体现的, 所以我们为了掌握正则表达式,必须要学习一些符号
虽然简化了,但阅读性差了
正则表达式用法演示:
正则表达式对字符串的常见操作:
正则表达式练习:
------- android培训、java培训、期待与您交流! ----------
正则表达式的特点:
正则表达式用于操作字符串数据
通过一些特定的符号来体现的, 所以我们为了掌握正则表达式,必须要学习一些符号
虽然简化了,但阅读性差了
正则表达式用法演示:
public class RegexDemo { public static void main(String[] args) { String qq="1237888"; // checkQQ(qq); String regex="[1-9][0-9]{4,14}";//正则表达式 boolean b=qq.matches(regex); System.out.println(qq+":"+b); } /* * 需求:定义一个功能对QQ好进行校验 * 要求:长度5-15,只能是数字,0不能开头 */ public static void checkQQ(String qq){ int len=qq.length(); if(len>=5 && len<=15){ if(!qq.startsWith("0")){ try{ long l=Long.parseLong(qq); System.out.println(l+":正确"); }catch(NumberFormatException e){ System.out.println(qq+":含有非法字符"); } }else{ System.out.println(qq+":不能0开头"); } }else{ System.out.println(qq+":长度错误!"); } } }
正则表达式对字符串的常见操作:
package com.itheima.regex; import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexDemo2 { public static void main(String[] args) { /* * 正则表达式对字符串的常见操作: 1.匹配 其实用的就是String类中的matches方法 * * 2.切割 其实就是用String类中的split方法 * * 3.替换 其实使用的就是String类中的replaceAll()方法 * * 4.获取 */ functionDemo_4(); } /* * 获取 1.将正则规则进行对象的封装:Pattern p =Pattern.compile("a*b"); * 2.通过正则对象的matcher方法字符串相关联,获取要对字符串操作的匹配器对象:Matcher m=p.matcher("aaaaaab"); * 3.通过matcher匹配器对象的方法对字符串进行操作:boolean b=m.matcher(); */ public static void functionDemo_4() { String str = "da jia hao,ming tian bu fang jia"; String regex = "\\b[a-z]{3}\\b"; // 1.将正则封装成对象 Pattern p = Pattern.compile(regex); // 2.通过正则对象获取匹配器对象 Matcher m = p.matcher(str); // 使用matcher对象的方法对字符串进行操作 // 既然要获取三个字母组成的单词 // 查找 find(); System.out.println(str); while (m.find()) { // System.out.println(m.group());// 获取匹配的子序列 System.out.println(m.start()+":"+m.end()); } } /* * 替换 */ public static void functionDemo_3() { String str = "zhangsantttttttxiaoqiangmmmmmmmzhaoliu"; str = str.replaceAll("(.)\\1+", "$1"); System.out.println(str); } /* * 切割 */ public static void functionDemo_2() { String str = "zhangsan xiaoqiang zhaoliu"; String[] names = str.split(" {1,}"); for (String name : names) { System.out.println(name); } } /* * 演示匹配 */ public static void functionDemo_1() { // 匹配手机号码是否正确 String tel = "1580001111"; String regex = "1[358][0-9]{9}"; boolean b = tel.matches(regex); System.out.println(tel + ":" + b); } }
正则表达式练习:
package com.itheima.regex; import java.util.TreeSet; public class RegexTest { public static void main(String[] args) { /* * 1.治疗口吃:我我...我我我...我我我.我我我.要..要要..要要要要要要....要要要要要要学学学..学编....编.编.......程程 * 2.Ip地址排序 * 3.对邮件地址校验 */ test_3(); } /* * 对邮件地址进行校验 */ public static void test_3() { String mail="abc1@sina.com.cn"; String regex="[a-zA-Z0-9_]+@[a-zA-Z0-9]+(\\.[a-zA-Z]{1,3})+"; boolean b=mail.matches(regex); System.out.println(mail+":"+b); } /* * 2.ip地址排序 * * 192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55 */ public static void test_2() { String ips_str="192.168.10.34 127.0.0.1 3.3.3.3 105.70.11.55"; //1.为了让ip可以按照顺序比较,只要让ip的没一段的位数相同, //补零,按照每一位所需在最多0进行补充。每一段都加2个零 ips_str=ips_str.replaceAll("(\\d+)", "00$1"); System.out.println(ips_str); ips_str=ips_str.replaceAll("\\d+(\\d{3})", "$1"); System.out.println(ips_str); //1.将ip地址切出 String[] ips=ips_str.split(" +"); TreeSet<String> ts=new TreeSet<String>(); for(String ip:ips){ ts.add(ip); } for(String ip:ts){ System.out.println(ip.replaceAll("(0)+", "$1")); } } /* * 1.治口吃 * */ public static void test_1(){ String str="我我...我我我...我我我.我我我.要..要要..要要要要要要....要要要要要要学学学..学编....编.编.......程程"; //1.将字符串中的.去掉,用替换 str=str.replaceAll("\\.+", ""); //2.去掉重复的字 str=str.replaceAll("(.)\\1+", "$1"); //打印结果 System.out.println(str); } }
------- android培训、java培训、期待与您交流! ----------
相关文章推荐
- 黑马程序员:正则表达式学习笔记
- 黑马程序员_java基础_正则表达式
- 黑马程序员_正则表达式
- 黑马程序员-----正则表达式
- 黑马程序员——自学总结(九)正则表达式
- 黑马程序员--正则表达式学习笔记
- 黑马程序员——正则表达式
- 黑马程序员_正则表达式
- 黑马程序员——正则表达式
- 黑马程序员——正则表达式3:练习
- 黑马程序员_正则表达式
- 黑马程序员_正则表达式
- 黑马程序员-正则表达式
- 黑马程序员_正则表达式
- 黑马程序员java学习笔记——正则表达式、反射
- 黑马程序员_正则表达式
- 黑马程序员_Java基础篇(二)——正则表达式
- 黑马程序员——javase基础--正则表达式
- 黑马程序员>>>Java正则表达式
- 黑马程序员——Java 基础 正则表达式(复习)