小菜鸟之正则表达式

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+"
";
 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
" +
 10 		"   临时 IPv6 地址. . . . . . . . . . : 2409:8920:850:5491:5c67:91ed:8962:b2e8
" +
 11 		"   本地链接 IPv6 地址. . . . . . . . : fe80::71b0:16c1:3c74:88fe%8
" +
 12 		"   IPv4 地址 . . . . . . . . . . . . : 192.168.43.221
" +
 13 		"   子网掩码  . . . . . . . . . . . . : 255.255.255.0
" +
 14 		"   默认网关. . . . . . . . . . . . . : fe80::ae92:32ff:fe6c:5da1%8
" +
 15 		"                                       192.168.43.232
" ;
 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:UsersAdministrator>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+"
";
 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
" +
  8 				"   子网掩码  . . . . . . . . . . . . : 255.255.255.0
" +
  9 				"   IPv4 地址 . . . . . . . . . . . . : 192.168.43.18
" +
 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