当前位置:网站首页>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);
}
}
边栏推荐
- Zhimei creative website exercise
- 广州市召开安全生产工作会议
- SQL head injection -- injection principle and essence
- ip2long之后有什么好处?
- Four functions of opencv
- 编译 libssl 报错
- 静态Vxlan 配置
- Common knowledge of one-dimensional array and two-dimensional array
- [learn microservice from 0] [01] what is microservice
- Master公式。(用于计算递归的时间复杂度。)
猜你喜欢
Visual stdio 2017 about the environment configuration of opencv4.1
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
JS to convert array to tree data
2022A特种设备相关管理(锅炉压力容器压力管道)模拟考试题库模拟考试平台操作
[爬虫]使用selenium时,躲避脚本检测
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
[pytorch practice] use pytorch to realize image style migration based on neural network
SQL lab 21~25 summary (subsequent continuous update) (including secondary injection explanation)
SQL Lab (41~45) (continuous update later)
[statistical learning method] learning notes - support vector machine (I)
随机推荐
SQL Lab (41~45) (continuous update later)
Four functions of opencv
SSM框架搭建的步骤
数据库安全的重要性
通讯协议设计与实现
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
用mysql查询某字段是否有索引
2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
HZOJ #240. 图形打印四
What is an esp/msr partition and how to create an esp/msr partition
[pytorch practice] use pytorch to realize image style migration based on neural network
Error in compiling libssl
解密GD32 MCU产品家族,开发板该怎么选?
How to apply @transactional transaction annotation to perfection?
[pytorch practice] write poetry with RNN
Day-18 hash table, generic
[statistical learning methods] learning notes - Chapter 5: Decision Tree
Vxlan static centralized gateway
2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation
What if does not match your user account appears when submitting the code?