当前位置:网站首页>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);
}
}
边栏推荐
- 【PyTorch实战】用RNN写诗
- Airserver automatically receives multi screen projection or cross device projection
- Multi row and multi column flex layout
- Cookie
- Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
- Session
- Common knowledge of one-dimensional array and two-dimensional array
- ip2long与long2IP 分析
- Day-20 file operation, recursive copy, serialization
- Preorder, inorder and postorder traversal of binary tree
猜你喜欢
【统计学习方法】学习笔记——支持向量机(上)
[statistical learning methods] learning notes - improvement methods
2022 polymerization process test question simulation test question bank and online simulation test
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
About sqli lab less-15 using or instead of and parsing
Static comprehensive experiment
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
NPM instal reports agent or network problems
3D content generation based on nerf
The left-hand side of an assignment expression may not be an optional property access. ts(2779)
随机推荐
[statistical learning methods] learning notes - improvement methods
爱可可AI前沿推介(7.7)
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
Realize a simple version of array by yourself from
AirServer自动接收多画面投屏或者跨设备投屏
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
【统计学习方法】学习笔记——提升方法
Charles: four ways to modify the input parameters or return results of the interface
How to use PS link layer and shortcut keys, and how to do PS layer link
Realize all, race, allsettled and any of the simple version of promise by yourself
Connect to blog method, overload, recursion
[Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
【二叉树】删点成林
【统计学习方法】学习笔记——支持向量机(下)
JS to convert array to tree data
Master公式。(用于计算递归的时间复杂度。)
MPLS experiment
ICLR 2022 | pre training language model based on anti self attention mechanism
聊聊Redis缓存4种集群方案、及优缺点对比
Several ways to clear floating