Java_MyRegex

来源:互联网 发布:学习软件营销方案 编辑:程序博客网 时间:2024/06/18 15:50
package com.tangguoqiang;import java.util.Arrays;import java.util.regex.Matcher;import java.util.regex.Pattern;/** * 正则表达式:符合一定规则的字符串 作用:匹配计算切割获取 规则:需要操作传入字符串的规律 java.util.regex *  * @author 唐国强 * */public class MyRegex {public static void main(String[] args) {/* 校验QQ * 长度5-15位 * 全部是数字 * 0不能开头 */checkQQ("01202561231234");checkQQByRegex("01202561231234");//匹配:使用String类中的matches()方法function1();//切割:使用String类的splist()方法function2();//替换:使用String类的replaceAll()方法function3();//获取:Patternfunction4();//ip地址排序function5();}private static void checkQQ(String qq) {int len = qq.length();if (len >= 5 && len <= 15) {if (!qq.startsWith("0")) {try {@SuppressWarnings("unused")long l = Long.parseLong(qq);} catch (NumberFormatException e) {e.printStackTrace();System.out.println("不全是数字");}} else {System.out.println("开头不能为0");}} else {System.out.println("长度不合法");}}private static void checkQQByRegex(String qq) {String regex = "[123456789][0-9]{4,14}";boolean matches = qq.matches(regex);if (matches) {System.out.println("正则表达式匹配成功");} else {System.out.println("正则表达式匹配失败");}}// 匹配private static void function1() {String tel = "13701336125";String regex = "1[358]\\d{9}";boolean matches = tel.matches(regex);System.out.println("正则匹配结果:" + matches);}// 切割private static void function2() {String str = "zhangsan  wangwu laoda";String[] split = str.split(" +");System.out.println("正则分割结果1:");for (String string : split) {System.out.println(string);}String str2 = "zhangsi.wangliu.daoer";String[] split2 = str2.split("\\.");System.out.println("正则分割结果2:");for (String string : split2) {System.out.println(string);}String str3 = "zhangsanyyyyyyywangwudddddddzhaoliu";String[] split3 = str3.split("(.)\\1+");System.out.println("正则分割结果3:");for (String string : split3) {System.out.println(string);}}// 替换private static void function3() {String str3 = "zhangsanyyyyyyywangwudddddddzhaoliu";// 目的是把叠词换成其中一个String replace = str3.replaceAll("(.)\\1+", "$1");System.out.println("正则替换结果:" + replace);// 电台抽奖Person 153****1235String tel = "15399993454";String replace2 = tel.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");System.out.println(replace2);}// 获取private static void function4() {// 将正则表达式封装成一个对象String str = "da jia hao ming tian xiu xi?";// 把三个连在一起组成的单词提取出来String regex = "\\b[a-z]{3}\\b";// 将正则表达式规则封装到Pattern对象并返回这个对象Pattern pattern = Pattern.compile(regex);// 用正则表达式调用获取匹配器的方法Matcher matcher = pattern.matcher(str);// 思想和HashMap的hasNext类似while (matcher.find()) {System.out.println(matcher.group());System.out.println(matcher.start() + ":" + matcher.end());}}private static void function5() {// ip地址排序System.out.println("ip地址排序:");String[] ips = { "10.2.4.23", "192.168.1.2", "173.68.46.65", "191.158.6.2", "9.2.4.23" };System.out.println("------1、补零------");for (int i = 0; i < ips.length; i++) {ips[i] = ips[i].replaceAll("(\\d+)", "00$1");System.out.println(ips[i]);}System.out.println("------2、截取------");for (int i = 0; i < ips.length; i++) {ips[i] = ips[i].replaceAll("0*(\\d{3})", "$1");System.out.println(ips[i]);}System.out.println("------3、排序------");Arrays.sort(ips);for (int i = 0; i < ips.length; i++) {System.out.println(ips[i]);}System.out.println("------4、去零------");for (int i = 0; i < ips.length; i++) {ips[i] = ips[i].replaceAll("0*(\\d+)", "$1");System.out.println(ips[i]);}}}