当前位置:网站首页>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 Lab (46~53) (continuous update later) order by injection
- 用mysql查询某字段是否有索引
- [爬虫]使用selenium时,躲避脚本检测
- 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)
- 图形对象的创建与赋值
- [Q&A]AttributeError: module ‘signal‘ has no attribute ‘SIGALRM‘
- gcc 编译报错
- Image pixel read / write operation
- HZOJ #240. 图形打印四
- 【深度学习】图像多标签分类任务,百度PaddleClas
猜你喜欢
On valuation model (II): PE index II - PE band
Preorder, inorder and postorder traversal of binary tree
Cookie
[pytorch practice] image description -- let neural network read pictures and tell stories
Static comprehensive experiment
Visual stdio 2017 about the environment configuration of opencv4.1
Routing strategy of multi-point republication [Huawei]
JS to convert array to tree data
SQL Lab (36~40) includes stack injection, MySQL_ real_ escape_ The difference between string and addslashes (continuous update after)
2022a special equipment related management (boiler, pressure vessel and pressure pipeline) simulated examination question bank simulated examination platform operation
随机推荐
【统计学习方法】学习笔记——支持向量机(下)
OSPF exercise Report
Cryptography series: detailed explanation of online certificate status protocol OCSP
test
SQL head injection -- injection principle and essence
有什么类方法或是函数可以查看某个项目的Laravel版本的?
Customize the web service configuration file
How to apply @transactional transaction annotation to perfection?
Session
在字符串中查找id值MySQL
Decrypt gd32 MCU product family, how to choose the development board?
[pytorch practice] write poetry with RNN
Importance of database security
[statistical learning methods] learning notes - Chapter 5: Decision Tree
Minimalist movie website
Charles: four ways to modify the input parameters or return results of the interface
MySQL导入SQL文件及常用命令
ACL 2022 | small sample ner of sequence annotation: dual tower Bert model integrating tag semantics
Connect to blog method, overload, recursion
[learn microservice from 0] [01] what is microservice