小菜鸟之正则表达式

image

image

image

image

image

 

  1 package day20190814_0001;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.util.regex.Matcher;
  7 import java.util.regex.Pattern;
  8 
  9 public class Test2 {
 10 	public static void main(String[] args) throws IOException, InterruptedException {
 11 //		String s="3546576sdfghcvsdfcgbn   ab3ab4ab5";
 12 //		String p="(.+?)((ab\\d)+)";
 13 //		
 14 		String buff=execCmd("ping www.baidu.com"); 
 15 		String ps="\\[(.+)\\]";
 16 
 17 		Pattern pt=Pattern.compile(ps);
 18 		Matcher m=pt.matcher(buff);
 19 		if(m.find()) {
 20 			System.out.println(m.group(1));
 21 		} else {
 22 			System.out.println("匹配失败");
 23 		}
 24 	}
 25 	public static String execCmd(String cmd) throws IOException, InterruptedException {
 26 		Runtime rt=Runtime.getRuntime();
 27 		Process p=rt.exec(cmd);
 28 		InputStreamReader isr=new InputStreamReader(p.getInputStream());
 29 		BufferedReader br=new BufferedReader(isr);
 30 		String ret="",line=null;
 31 		while((line=br.readLine())!=null) {
 32 			ret+=line+"\n";
 33 			Thread.sleep(10);
 34 		}
 35 		br.close();
 36 		return ret;
 37 	}
 38 }
 39 
 
  
 
 
  1 package 正则表达式;
  2 
  3 import java.util.regex.*;//表示正则表达式,所有包
  4 
  5 public class Test {
  6 
  7 	public static void main(String[] args) {
  8 		// TODO Auto-generated method stub
  9 String s="IPv6 地址 . . . . . . . . . . . . : 2409:8920:850:5491:71b0:16c1:3c74:88fe\r\n" +
 10 		"   临时 IPv6 地址. . . . . . . . . . : 2409:8920:850:5491:5c67:91ed:8962:b2e8\r\n" +
 11 		"   本地链接 IPv6 地址. . . . . . . . : fe80::71b0:16c1:3c74:88fe%8\r\n" +
 12 		"   IPv4 地址 . . . . . . . . . . . . : 192.168.43.221\r\n" +
 13 		"   子网掩码  . . . . . . . . . . . . : 255.255.255.0\r\n" +
 14 		"   默认网关. . . . . . . . . . . . . : fe80::ae92:32ff:fe6c:5da1%8\r\n" +
 15 		"                                       192.168.43.232\r\n" ;
 16 String p="IPv4 地址 (\\. )+: ";//+表示至少一个,准确值
 17 p+="((\\d{1,3}\\.){3}\\d{1,3})";
 18 Pattern pt=Pattern.compile(p);//计算机语言参照物
 19 Matcher m=pt.matcher(s);//一个方法
 20 String masks="子网掩码*(\\.)+: ";
 21 masks="((\\d{1,3}\\.){3}\\d{1,3})";
 22 Pattern maskspt=Pattern.compile(masks);//计算机语言参照物
 23 Matcher masksm=pt.matcher(s);
 24 
 25 //System.out.println(m.matches());//完全匹配
 26 //System.out.println(m.lookingAt());//开头匹配
 27 //System.out.println(m.find());//可以连续选择
 28 //System.out.println(m.start()+"~"+m.end()+":"+m.group());//起始下标,
 29 	m.find();//对于下标,从左向右数,下标0是整体,
 30 	String g=m.group();
 31 	String g0=m.group(0);
 32 	String g1=m.group(1);
 33 	String g2=m.group(2);
 34 	String g3=m.group(3);
 35 	System.out.println("g"+g);
 36 	System.out.println("g0"+g0);
 37 	System.out.println("g1"+g1);
 38 	System.out.println("g2"+g2);
 39 	System.out.println("g3"+g3);
 40 }
 41 }

 

  1 package 正则;
  2 
  3 import java.io.BufferedReader;
  4 import java.io.IOException;
  5 import java.io.InputStreamReader;
  6 import java.util.regex.Matcher;
  7 import java.util.regex.Pattern;
  8 
  9 public class Test2 {
 10 	public static void main(String[] args) throws IOException, InterruptedException {
 11 //		String s="3546576sdfghcvsdfcgbn   ab3ab4ab5";
 12 //		String p="(.+?)((ab\\d)+)";
 13 //		
 14 		String buff=execCmd("ping www.baidu.com");
 15 		String ps="\\[(.+)\\]";//正则表达式
 16 //		(c) 2018 Microsoft Corporation。保留所有权利。
 17 
 18 //C:\Users\Administrator>ping www.baidu.com
 19 //
 20 //正在 Ping www.a.shifen.com [183.232.231.174] 具有 32 字节的数据:
 21 //来自 183.232.231.174 的回复: 字节=32 时间=71ms TTL=53
 22 //来自 183.232.231.174 的回复: 字节=32 时间=59ms TTL=53
 23 //来自 183.232.231.174 的回复: 字节=32 时间=62ms TTL=53
 24 //来自 183.232.231.174 的回复: 字节=32 时间=63ms TTL=53
 25 
 26 		Pattern pt=Pattern.compile(ps);//将字符串编译程Pattern对象
 27 		Matcher m=pt.matcher(buff);//使用Pattern对象创建Matcher对象
 28 		if(m.find()) {
 29 			System.out.println(m.group(1));//如果找到
 30 		} else {
 31 			System.out.println("匹配失败");
 32 		}
 33 	}//java运行windows命令
 34 	public static String execCmd(String cmd) throws IOException, InterruptedException {
 35 		Runtime rt=Runtime.getRuntime();
 36 		Process p=rt.exec(cmd);
 37 		InputStreamReader isr=new InputStreamReader(p.getInputStream());
 38 		BufferedReader br=new BufferedReader(isr);
 39 		String ret="",line=null;
 40 		while((line=br.readLine())!=null) {
 41 			ret+=line+"\n";
 42 			Thread.sleep(10);
 43 		}
 44 		br.close();
 45 		return ret;
 46 	}
 47 }
 48 

 

 

 

 

  1 package 正则
;
  2 
  3 import java.util.regex.*;
  4 
  5 public class Test {
  6 	public static void main(String[] args) {
  7 		String s="   本地链接 IPv6 地址. . . . . . . . : fe80::1ce3:be28:4c88:c72e%15\r\n" +
  8 				"   子网掩码  . . . . . . . . . . . . : 255.255.255.0\r\n" +
  9 				"   IPv4 地址 . . . . . . . . . . . . : 192.168.43.18\r\n" +
 10 				"   默认网关. . . . . . . . . . . . . : fe80::bee2:65ff:fe45:7530%15";
 11 		String ipv4s="IPv4 地址 +(\\. )+: "\\
 12 		ipv4s+="((\\d{1,3}\\.){3}\\d{1,3})"\\这是正则的一种表达式
 13 		Pattern ipv4pt=Pattern.compile(ipv4s);
 14 		Matcher ipv4m=ipv4pt.matcher(s);
 15 		String masks="子网掩码 *(\\. )+: ";
 16 		masks+="((\\d{1,3}\\.){3}\\d{1,3})";
 17 		Pattern maskpt=Pattern.compile(masks);
 18 		Matcher maskm=maskpt.matcher(s);
 19 		ipv4m.find();maskm.find();
 20 		String ip=ipv4m.group(2);
 21 		String mask=maskm.group(2);
 22 		System.out.println("ip:"+ip+"       mask:"+mask);
 23 	}
 24 }
 25 输出:
ip:192.168.43.18       mask:255.255.255.0

转载于:https://www.cnblogs.com/czg-0705/p/11354938.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值