Day_24_正则表达式

UDP使用:

public class UDP_send {
	public static void main(String[] args) throws IOException {
		// 要发送的数据
		Scanner s = new Scanner(System.in);
		String string = s.nextLine();
		// 数组输出流
		ByteArrayOutputStream b = new ByteArrayOutputStream();
		// 创建数据流,流向b
		DataOutputStream d = new DataOutputStream(b);
		// 写出数据,把数据写到字节数组输出流,保存数据类型
		d.writeUTF(string);
		// 将b中的字符串转化为字节数组
		byte[] by = b.toByteArray();
		// 数据传输
		// 创建数据包,把数据打包
		// 绑定服务端地址和端口
		DatagramPacket dp = new DatagramPacket(by, by.length,
				new InetSocketAddress("127.0.0.1", 10000));

		// 本地开端口发数据。。开端口
		DatagramSocket ds = new DatagramSocket(9999);
		// 发送数据
		ds.send(dp);
		ds.close();
		System.out.print("发送完成");
	}
}


public class UDP_accact {
	public static void main(String[] args) throws IOException {
		// 监听一个端口,数据会传入这个端口,接受数据
		DatagramSocket d = new DatagramSocket(10000);
		byte[] by = new byte[1024];
		// 数据打包,打包数组,将数组给包相当于开了这么大空间
		DatagramPacket p = new DatagramPacket(by, by.length);
		while (true) {
			// 通过开启的端口接受数据
			d.receive(p);
			// 单纯的字节数组缓冲输入流
			ByteArrayInputStream ba = new ByteArrayInputStream(by);
			// 单纯的字节输入流
			DataInputStream da = new DataInputStream(ba);
			// 读出来
			System.out.println(da.readUTF());
		}
	}
}

线程,死锁,案例:

public class Text_ {
	public static void main(String[] args) {
		Window window = new Window();
		Thread t1 = new Thread(new xian(window));
		Thread t2 = new Thread(new xian(window));
		Thread t3 = new Thread(new xian(window));
		t1.setName("t1");
		t2.setName("t2");
		t3.setName("t3");
		t1.start();
		t2.start();
		t3.start();
	}
}

class xian implements Runnable {
	// 造一个对象
	Window window;

	// 构造方法,传参的时候确定参数
	public xian(Window window) {
		this.window = window;
	}

	// 线程
	public void run() {
		while (window.piao >= 0) {
			window.pop();
		}
	}
}

class Window {
	// 票数
	int piao = 100;

	// 取票的方法
	// 锁定的是线程对象,而不是线程中所用的类的对象(window)所有线程都是用的一个window,所以能改变同一个window对象中的数据
	public synchronized void pop() {
		// 票为0,结束循环
		if (piao <= 0) {
			return;
		}
		// 取票
		piao--;
		try {
			Thread.sleep(100);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(Thread.currentThread().getName() + " 取票成功 , 剩余 "
				+ piao + " 票");
	}

 

正则表达式:

 *       正则表达式定义了字符串的模式,可以用来搜索编辑,处理文本,并不仅限于某一种语言
 * 
 *       保存在java.util.regex包下
 * 
 *       正则表达式常用语法:
 * 
 *       \ :转移符,把有意义字符转换为无意义字符
 * 
 *       范围相关(匹配一个):
 * 
 *       [abc] :可能是a,可能是b,也可能是c
 * 
 *       [a-z] +:可以匹配到小写字母
 * 
 *       [A-Za-z] :可以匹配到大小写字母
 * 
 *       [A-Za-z0-9] :大小写字母和数字
 * 
 *       [0-9] :匹配数字 [^0-9] :匹配不是数字!@#ax|
 * 
 *       简洁表示方式:
 * 
 *       .:匹配任意字符,如果想匹配.需要转义\.
 * 
 *       \d:表示数字等价于[0-9] \D :并表示非数字[^0-9]
 * 
 *       \s :表示由空字符组成 \S :表示非空
 * 
 *       \w :表示字母,数字,下划线[0-9a-zA-Z_ ] \W :表示非字母数字下划线
 * 
 *       数量相关:
 * 
 *       ? :表示出现0次或1次
 * 
 *       + :表示出现1次或多次,大于等于1
 * 
 *       * :表示出现大于等于0
 * 
 *       {n} :表示出现n次, [0-9|{6} :表示出现6位数字
 * 
 *       {n,} :表示出现n次或n次以上,就是大于等于n,\d{6} :表示出现至少6个数字
 * 
 *       {n,m} :表示出现n到m次, \d{6,9} :表示出现6到9个数字
 * 
 * 
 * 
 *       |:或,x|y表示出现x或者是y
 * 
 *       ():子表达式,看做一个整体 ([0-9]张){5}
 * 
 * 
 *       匹配整数和小数 ^\d+(\.\d+)?$
 * 
 *       ^表示开始,$表示结尾
 * 
 *       一般想要强制全词匹配的时候,才加^和$
 * 
 *       \d :匹配数字 + :匹配一个或多个
 * 
 *       (\.\d+) :把\.和\d+看做整体
 * 
 *       ? :只能出现0次或1次
 * 
 * 
 * 
 *       java中有三个正则表达式相关的类
 * 
 *       PatternSyntaxException :正则表达式异常类
 * 
 *       (私有)Pattern :正则表达式类
 * 
 *       (私有)Matcher :支持较强大的正则表达式匹配操作
 * 
 * 
 * 
 *       实际操作中,有时候简单的校验操作,可以直接使用String的
 * 
 *       验证: boolean matches(String regex)
 * 
 *       拆分: String[] split(String regex) 按regex拆
 * 
 *       替换: String replacaAll(String regex,String str)
 * 

public class ReGex_ {
	public static void main(String[] args) {

	}
//匹配
	public static void text2(){
		String string = "535.45";
		//匹配小数
		String regex = "\\d+(\\.\\d+)?";
		//调整匹配行为
		Pattern pattern = Pattern.compile(regex);
		//判断是否匹配,并且是全词匹配
		boolean result = pattern.matches(regex, string);
		System.out.println(result);
	}
	
	//拆分
	public static void text1() {
		String string = "1.5.2.6.6.5";
		// 创建正则表达式引擎对象
		// 注意,所有以 . 进行操作的 都要转义,而在java中 使用正则表达式的转移符 要写两个
		// 因为在java中\ 也是转移符
		// Pattern.compile("flag") 它接受一个标记参数flag,以调整匹配的行为
		Pattern pattern = Pattern.compile("\\.");
		// 根据conmpile的参数来分割
		String[] str = pattern.split(string);
		for (String string2 : str) {
			System.out.println(string2);
		}

		// string 中的拆分
		String[] str3 = string.split("\\.");
		for (String string2 : str) {
			System.out.println(string2);
		}
	}

}

Matcher. matches() :对整个字符串进行匹配,只有整个字符串都匹配了才返回true
Mather. lookingAt() :对前面的字符串进行匹配,只有匹配到的字符串在最前面才返回true
Matcher. find() :对字符串进行匹配,匹配到的字符串可以在任何位置
 

public class Matcher_01 {
	public static void main(String[] args) {
		test1();
		test2();
		test3();
		test4();
	}

	// 1 matches : 全词匹配
	public static void test1() {
		// 字符和正则表达式
		String input = "191919191";
		String regex = "\\d{19}";
		// 创建正则表达式引擎对象
		Pattern pattern = Pattern.compile(regex);
		// 创建匹配器,接受匹配参数
		Matcher matcher = pattern.matcher(input);
		// 全词匹配
		boolean re = pattern.matches(regex, input);
		System.out.println(re);
	}

	// 2 lookingAt : 从前往后匹配,前面符合条件就行
	public static void test2() {
		String input = "13113113111asd";
		String regex = "\\d{11}";

		// 创建正则表达式引擎对象
		Pattern pattern = Pattern.compile(regex);
		// 创建匹配器
		Matcher matcher = pattern.matcher(input);
		// 匹配
		boolean result = matcher.lookingAt();
		System.out.println(result);
	}

	// 3 find : 任意位置符合条件都可以
	public static void test3() {
		String input = "asd13113113111asd";
		String regex = "\\d{11}";

		// 创建正则表达式引擎对象
		Pattern pattern = Pattern.compile(regex);
		// 创建匹配器
		Matcher matcher = pattern.matcher(input);
		// 匹配
		boolean result = matcher.find();
		System.out.println(result);
	}

	// 4 group : find和group一起使用,可以做到数据提取
	public static void test4() {
		String input = "张小三的电话号码是13113113111s@##李四的电话号码是13113113112王五的电话号码是13113113113";
		// [\u4E00-\u9FFF] 汉字范围
		String regex = "([\u4E00-\u9FFF]{2,3})的电话号码是(\\d{11})";
		// 创建正则表达式引擎对象
		Pattern pattern = Pattern.compile(regex);
		// 创建匹配器
		Matcher matcher = pattern.matcher(input);
		// 匹配
		while (matcher.find()) {
			// group() 和 group(0) 都是提取匹配到的数据
			// 1 就是第一组数据(第一个小括号) , 2 就是第二组数据
			// System.out.println(matcher.group());
			// System.out.println(matcher.group(0));
			System.out.println(matcher.group(1) + " : " + matcher.group(2));
		}
	}
}

字符去重案例:

public class Test {
	public static void main(String[] args) {

		// 还原成 : 我要学编程
		String input = "我我...我我...我要..要要...要要...学学学....学学...编编编..编程..程.程程...程...程";
		// 1 把.去掉
		input = input.replaceAll("[^\u4E00-\u9FFF]", "");
		// 我我我我我要要要要要学学学学学编编编编程程程程程程
		System.out.println(input);

		// (.) : 任意字符组成
		// \\1 获取前面组中的数据
		// (\\d)\\1 : 表示两个连续出现的数字, 比如 11,22,33,44
		// (\\d)(a)\\1 : 表示 第一个和第三个是相同的数字,且数字中间有个a ,1a1,9a9
		// (\\d)(a)\\2 : 表示 第一个是数字,第二个和第三个都是a,1aa,3aa
		String regex = "(.)(\\1+)";

		// 创建正则表达式引擎对象
		Pattern pattern = Pattern.compile(regex);
		// 创建匹配器
		Matcher matcher = pattern.matcher(input);
		// find查找
		// while (matcher.find()) {
		// // 使用 group(1)把group(0) 替换即可
		// input = input.replaceAll(matcher.group(), matcher.group(1));
		// }

		// $1 就是 group(1) 而 regex 就等于是group();
		input = input.replaceAll(regex, "$1");
		System.out.println(input);
	}
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值