当前位置:网站首页>Day-24 UDP, regular expression
Day-24 UDP, regular expression
2022-07-07 12:48:00 【Xiaobai shelter】
1.1 UDP
Server side
public class UDPServer {
public static void main(String args[])throws Exception{
// open UDP object , And listen to a port , For receiving data
DatagramSocket ds=new DatagramSocket(10000);
// Create an array of bytes , Used to save the received data
byte[] buf=new byte[1024];
// Declare a packet receiver
DatagramPacket dp=new DatagramPacket(buf,buf.length);
while(true){
// Receive data through the open port
ds.receive(dp);
ByteArrayInputStream bais=new ByteArrayInputStream(buf);
DataInputStream dis =new DataInputStream(bais);
System.out.println(dis.readUTF());
}
}
}
client
Here public class UDPClient {
public static void main(String args[])throws Exception{
DatagramSocket ds=new DatagramSocket(9999);
boolean flag=true;
while(flag){
Scanner scanner =new Scanner(System.in);
// Data to send
String string =scanner.nextLine();
// Create byte array output stream
ByteArrayOutputStream baos=new ByteArrayOutputStream();
// Create data flow
DataOutputStream dos=new DataOutputStream(baos);
// Writing data , Write data to byte array stream , At the same time, the data type is saved
dos.writeUTF(string);
// Convert data into a byte array
byte[] buf=baos.toByteArray();
// System.out.println(new String(buf));
// The data transfer
// Create a package , Package the data
// And bind the server address and port
DatagramPacket dp=new DatagramPacket(buf,buf.length,new InetSocketAddress("192.168.0.157",10000));
// transmission , You need to open the port locally to send data
// Send packet
ds.send(dp);
// close resource
System.out.println(" Send complete ");
}
ds.close();
}
} Insert a piece of code into
2. Regular expressions
Regular expressions Defines the pattern of the string , It can be used to search for , edit , Processing text , It's not limited to one language
But there are subtle differences in every language
jdk1.4 Introduce regular expressions , Save in java.util.regex It's a bag
Common grammar
Regular expression syntax :
* \: Escape character , Convert meaningful characters into meaningless characters
* Scope related
* [abc]: May be a, May be b, It could be c
* [a-z]: Can match to lowercase letters
* [A-Za-z]: Can match to upper and lower case letters
* [A-Za-z0-9]: Upper and lower case letters and numbers
* [0-9]: Match the Numbers
* [^0-9]: Matching is not a number
* Concise representation
* .: Match any character , If they match . Need to escape \.
* \d: Representation number Equivalent to [0-9]
* \D: It means not a number [^0-9]
* \s: Indicates that it consists of empty characters
* \S: It means not empty
* \w: For letters , Numbers , Underline Equivalent to [0-9a-zA-Z]
* \W: Indicates a non alphanumeric underscore
* Quantitative correlation :
* ?: Appear 0 Time or 1 Time
* +: Appear 1 Times or times , Greater than or equal to 1
* *: Greater than or equal to 0
* {
n}: Appear n Secondary bit ,[0-9]{
6}: Appear 6 Digit number
* {
n,}: Appear n Time or n More than once , It's greater than or equal to n,\d{
6,}: Indicates that at least 6 A digital
* {
n,m}: Appear n To m Time ,\d{
6,9}: Appear 6 To 9 A digital
* |: or ,x|y Appear x Or is it y
* (): subexpression , As a whole ([0-9] Zhang ){
5}
* Match integers and decimals
* ^\d+(\.\d+)?$
* ^: To begin , With xxx Lead
* $: End of the said
* Generally, when you want to force full word matching , Cai Jia ^ and $
* \d: Match the Numbers
* +: Match one or more
* (\.\d+): hold \. and \d+ As a whole
* ?: Can only appear 0 Time or 1 Time
* [a-zA-Z_$][a-zA-Z0-9_$]*
*java There are three regular expression related classes in
* PatternSyntaxException: Regular expression exception class
* Pattern: regular expression class
* Matcher: Support more powerful regular expression matching operations
*
* In practice , Sometimes simple verification operations , You can use it directly String Of
* verification :boolean matches(String regex)
* Split :String[] spilt(String regex)
* Replace :String replacaAll(String regex,String str)
*
*/
public class Pattern_01 {
public static void main(String args[]){
test2();
}
// Match method
public static void test2(){
// Content
String str="23.456";
// Regular expressions
String regex="\\d+(\\.\\d+)?";
// Create engine objects
Pattern pattern=Pattern.compile(regex);
// matching , And it's a whole word match
boolean result=pattern.matches(regex, str);
System.out.println(result);
}
// Split method
public static void test1(){
String string="1.2.3.4.5";
// Create a regular expression engine object
// Be careful , So . All operations should be escaped , And in the java Using the escape character of regular expression in, you need to write two
// Because in java in \ It's also an escape character
Pattern pattern=Pattern.compile("\\.");
// Call the split method
String[] arr=pattern.split(string);
for(String string2:arr){
System.out.println(string2);
}
//String Split in
arr=string.split("\\.");
for(String string2:arr){
System.out.println(string2);
}
}
}
Matcher And data extraction
Here public class Matcher_02 {
public static void main(String args[]){
test1();
test2();
test3();
test4();
}
//1 matches: Whole word matching
public static void test1(){
String input="13113113111";
String regex="\\d{11}";
// Create a regular expression engine object
Pattern pattern=Pattern.compile(regex);
// Create matchers
Matcher matcher=pattern.matcher(input);
// matching
boolean result=matcher.matches();
System.out.println(result);
}
//2 lookingAt: Match from front to back , Just meet the conditions ahead
public static void test2(){
String input="19931173467dafafa";
String regex="\\d{11}";
// Create a regular expression engine object
Pattern pattern=Pattern.compile(regex);
// Create matchers
Matcher matcher=pattern.matcher(input);
// matching
boolean result=matcher.lookingAt();
System.out.println(result);
}
//3 find: Any position that meets the conditions can
public static void test3(){
String input="daddua19931173467afa";
String regex="\\d{11}";
// Create a regular expression engine object
Pattern pattern=Pattern.compile(regex);
// Create matchers
Matcher matcher=pattern.matcher(input);
// matching
boolean result =matcher.find();
System.out.println(result);
}
//4 group:find and group Use it together , Can do data extraction
public static void test4(){
String input=" Zhang Xiaosan's telephone number is [email protected]!# What's Li Si's phone number 13115467894 Wang Wu's telephone number is 12356478569";
// The range of Chinese characters [\u4E00-\u9FFF]
String regex="([\u4E00-\u9FFF]{2,3}) Your phone number is (\\d{11})";
// Create a regular expression engine object
Pattern pattern=Pattern.compile(regex);
// Create matchers
Matcher matcher=pattern.matcher(input);
// matching
while(matcher.find()){
//group() and group(0) It's all about extracting matching data
//1 Is the first set of data ( The first parenthesis ),2 Is the second set of data
//System.out.println(matcher.group());
// System.out.println(matcher.group(0));
System.out.println(matcher.group(1)+":"+matcher.group(2));
}
}
} Insert a piece of code into
Reduplicate words
* subject :
* Take me to ... Me me ... I want to .. Yes, yes ... Yes, yes ... Learn to learn .... Science ... Make up .. Programming .. cheng . Cheng Cheng ... cheng ... cheng
* restore : I want to learn programming
*/
public class Test_delect {
public static void main(String args[]){
String input=" Me me ... Me me ... I want to .. Yes, yes ... Yes, yes ... Learn to learn .... Science ... Make up .. Programming .. cheng . Cheng Cheng ... cheng ... cheng ";
//1 hold . Get rid of Alternative method , Replace the one before the comma with the one after the comma
input=input.replaceAll("[^\u4E00-\u9FFF]", "");
// I'm going to learn to program
System.out.println(input);
//(.): Any character
//\\1 Get the data in the previous group
//(\\d)\\1: Represents two consecutive numbers , such as 11,22,33,44
//(\\d)(a)\\1: Indicates that the first and third are the same number , And there is a... In the middle of the number a,1a1,9a9
//(\\d)(a)\\2: Indicates that the first is a number , The second and third are both a,1aa,3aa
String regex="(.)(\\1+)";
// Create a regular expression engine object
Pattern pattern=Pattern.compile(regex);
// Create matchers
Matcher matcher=pattern.matcher(input);
//find lookup
while(matcher.find()){
// Use group(1) hold group(0) Replace it
input=input.replaceAll(matcher.group(), matcher.group(1));
}
System.out.println(input);
//$1 Namely group(1) and regex It's like group();
input=input.replaceAll(regex, "$1");
System.out.println(input);
}
}
边栏推荐
- SQL head injection -- injection principle and essence
- Routing strategy of multi-point republication [Huawei]
- [learn wechat from 0] [00] Course Overview
- HZOJ #235. 递归实现指数型枚举
- leetcode刷题:二叉树21(验证二叉搜索树)
- Visual stdio 2017 about the environment configuration of opencv4.1
- 如何将 @Transactional 事务注解运用到炉火纯青?
- Day-15 common APIs and exception mechanisms
- leetcode刷题:二叉树19(合并二叉树)
- Day-19 IO stream
猜你喜欢
Connect to blog method, overload, recursion
基于NeRF的三维内容生成
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
What if does not match your user account appears when submitting the code?
opencv的四个函数
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
[statistical learning method] learning notes - support vector machine (I)
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
Routing strategy of multi-point republication [Huawei]
随机推荐
Static routing assignment of network reachable and telent connections
ACL 2022 | 序列标注的小样本NER:融合标签语义的双塔BERT模型
【PyTorch实战】用RNN写诗
【从 0 开始学微服务】【03】初探微服务架构
Aike AI frontier promotion (7.7)
【统计学习方法】学习笔记——第五章:决策树
Four functions of opencv
【深度学习】图像多标签分类任务,百度PaddleClas
ICLR 2022 | 基于对抗自注意力机制的预训练语言模型
[binary tree] delete points to form a forest
静态Vxlan 配置
【统计学习方法】学习笔记——支持向量机(上)
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
有什么类方法或是函数可以查看某个项目的Laravel版本的?
在字符串中查找id值MySQL
Utiliser la pile pour convertir le binaire en décimal
Polymorphism, final, etc
How to use PS link layer and shortcut keys, and how to do PS layer link