正则表达式是用来描述具有一定特征的字符串的特殊字符串
验证、查找、替换、分割 。
 普通字符 精确匹配
 元字符 [] () \ ^ $ . * + ? |
 转义字符 \d \ \s \w 等
 1.4.1 自定义 []
 1) ^ 如果在第一个位置,表示取反的含义。
 2) -表示一个区间即范围、
 3) ] 最近一个位置为:结束,表示普通 [
 4) .自身.的含义。如果需要表示原有的含义 1.4.2 默认|标准字符类
 1) \d [0,9]
 2) \w [a-zA-Z0-9]
 3) \s 空格 tab 换行
 4) . 可以匹配任意字符
 1) * 0个及以上
 2) + 1个及以上
 3) ? 0或1个
 4) {n} n次,非负数
 5) {n,} 大于等于n次
 6) {n,m} 大于等于n,小于等于m次
 1) * {n,} +贪婪模式 (匹配字符越多越好,可回溯)
 2) ?懒惰模式 (匹配字符越少越好,可回溯)
 3) +独占模式 (匹配字符越多越好,不可回溯)
 4) 阻止贪婪有两种方式: 量词后面使用? 使用取反
 1) 边界不占用宽度,只是一个界限
 2) ^ :开始 \b:单词边界 \B:非单词边界 $:结束
 ^:多行代表每行头 单行代表整个字符串的开始
 $: 多行代表每行尾 单行代表字符串的结尾
 \b 匹配前面或后面的不是\w
 \B 匹配前面或后面的是\w
 1) 选择组:| 优先级低 ,满足匹配则停止,不会查找更优的方案
 2) 分组:() 反向引用\1 \2… 非捕获组(?:xxx)
 1) i:insensitive 使正则表达式对大小写不敏感;(重点)
 2) s:singleline 开启“单行模式”,即点号“.”匹配新行符;
 3) m: multiline 开启“多行模式”,即“^”和“$”匹配新行符的前面和后面的位置
 4) (?i)select(?-i) -> 不区分大小写。
1.10 零宽断言
 1) (?=exp) 先行断言
 2) (?<=exp) 后发断言
1.11 常用类
 Pattern compile() 将给定的正则表达式编译到模式中
 Matcher matcher() find() group() matches()
 replaceAll() spilt()
public class Demo01Regx { public static void main(String[] args) { String str = "hku89q3429ureisdfj38we"; //匹配三个数字 Pattern p = Pattern.compile("\\d{2}"); //在字符串中匹配 Matcher m = p.matcher(str); //System.out.println(m.group());//获取找到的内容 System.out.println(m.find());//true str = "djgrugi4jhwrh3rhk3rht8er"; str = str.replaceAll("\\d","-"); System.out.println(str);//djgrugi-jhwrh-rhk-rht-er } } InetAddress
getLocalHost() getByName(计算机名|域名|ip地址)
getHostAddress() getHostName()
public static void main(String[] args) throws UnknownHostException { //获取本机的地址(计算机名+ip地址) InetAddress localHost = InetAddress.getLocalHost(); System.out.println(localHost);//LAPTOP-QR5KADD9/192.168.11.195 //获取其他计算机的地址 局域网中的计算机名,域名,ip地址 InetAddress address = InetAddress.getByName("DESKTOP-V47VTGS"); System.out.println(address);//DESKTOP-V47VTGS/192.168.11.168 //获取外网中的电脑 address = InetAddress.getByName("www.baidu.com"); System.out.println(address);//www.baidu.com/180.101.49.11s //getHostAddress() 返回 ip 地址 //getHostName() 返回域名|本机为计算机名 } public static void main(String[] args) throws Exception { //百度搜索首页URL URL url = new URL("http://www.baidu.com"); //输入流 BufferedInputStream bis = null; //创建文件联系 File file = new File("baidu.html"); //输出流 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file)); bis = new BufferedInputStream(url.openStream()); int len; byte[] car = new byte[1024]; while ((len = bis.read(car))!=-1){ bos.write(car,0,len); } bos.flush(); bis.close(); bos.close(); } TCP(transfer control protocol) 打电话 面向连接、安全、可靠,效率低
服务器端
 ①创建服务器 指定端口
 创建服务器ServerSocket,在创建时定义ServerSocket的监听端口(在这个端口接收客户端发来的消息!)
 ②等待客户端连接 ServerSocket调用accept()方法,使之处于阻塞状态
 ③分析接收数据 利用Socket进行接收和发送数据
客户端
 ①连接服务器: 创建客户端 +指定服务器地址 +端口
 创建客户机Socket,并设置服务器的ip及端口,客户机发出连接请求,建立连接。
 ②发送数据
 通过Socket发送数据,和接收数据
public static void main(String[] args) throws Exception { //服务器端套接字 ServerSocket socket = new ServerSocket(8888); //监听 等待客户端请求连接 Socket client = socket.accept(); //输入流 BufferedInputStream bis = new BufferedInputStream(client.getInputStream()); //输出流 BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream()); //接受数据的空间 byte[] car = new byte[1024]; //读取|接收数据 int len = bis.read(car); System.out.println(new String(car, 0, len));//哈啊哈哈 //发送数据 bos.write("收到消息了".getBytes()); bos.flush(); //关闭 bos.close(); bis.close(); socket.close(); } public static void main(String[] args) throws Exception { //创立连接 目标主机地址 端口号 Socket server = new Socket(InetAddress.getLocalHost(), 8888); BufferedOutputStream bos = new BufferedOutputStream(server.getOutputStream()); BufferedInputStream bis = new BufferedInputStream(server.getInputStream()); //发送消息 bos.write("哈啊哈哈".getBytes()); bos.flush(); byte[] car = new byte[1024]; int len = bis.read(car); System.out.println("从服务器端接收的消息:" + new String(car,0,len)); //从服务器端接收的消息:收到消息了 bis.close(); bos.close(); server.close(); }UDP(UserDatagramProtocol ) 发送短信 非面向连接、不安全、数据可能丢失 、效率高
 DatagramSocket:用于发送或接收数据包
 DatagramPacket:数据容器(封包)
服务器端:
 ①创建服务器 DatagramSocket类 +指定端口 (定义服务器端的监听端口)
 ②准备接收容器 字节数组 +封装成DatagramPacket数据包 (准备容器接收数据)
 ③接收数据
 ④分析数据
 ⑤释放资源
客户端:
 ①创建客户端 DatagramSocket类 +指定端口 (定义客户端的监听端口)
 ②准备数据 字节数组
 ③封装成数据包 需要指定包发送的地址+端口 即服务器地与端口 (打包要发送的数据)
 ④发送数据
 ⑤释放资源
public static void main(String[] args) throws Exception { //接收数据包 制定自己的端口号为8888 DatagramSocket socket = new DatagramSocket(8888); //接受数据的空间 byte[] car = new byte[1024]; //数据容器(封包) 接收的容器 DatagramPacket packet = new DatagramPacket(car, car.length); //接收 socket.receive(packet); //打印接收内容 String str = new String(car, 0, packet.getLength()); System.out.println(str);//哈哈哈哈哈 socket.close(); } public static void main(String[] args) throws Exception { //用于发送数据包 指定自己的端口 DatagramSocket socket = new DatagramSocket(9999); //发送的数据 byte[] data = "哈哈哈哈哈".getBytes(); //数据包 里面存放了发送的数据 发送的地址 端口 DatagramPacket packet = new DatagramPacket(data, 0, data.length, InetAddress.getLocalHost(), 8888); //发送 socket.send(packet); System.out.println("已发送"); socket.close(); }
