当前位置:网站首页>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);
}
}
边栏推荐
- 【从 0 开始学微服务】【02】从单体应用走向服务化
- [learn microservice from 0] [01] what is microservice
- [statistical learning method] learning notes - logistic regression and maximum entropy model
- About sqli lab less-15 using or instead of and parsing
- SSM框架搭建的步骤
- Importance of database security
- 图像像素读写操作
- The IDM server response shows that you do not have permission to download the solution tutorial
- [binary tree] delete points to form a forest
- OSPF exercise Report
猜你喜欢

Cookie

图像像素读写操作

3D content generation based on nerf
![[crawler] avoid script detection when using selenium](/img/3a/85ea729be2aa76c3de4a822ca6939b.png)
[crawler] avoid script detection when using selenium

Cookie

Zhimei creative website exercise

Static comprehensive experiment
![[pytorch practice] image description -- let neural network read pictures and tell stories](/img/39/b2c61ae0668507f50426b01f2deee4.png)
[pytorch practice] image description -- let neural network read pictures and tell stories

数据库安全的重要性

2022 examination questions and online simulation examination for safety production management personnel of hazardous chemical production units
随机推荐
编译 libssl 报错
[difficult and miscellaneous]pip running suddenly appears modulenotfounderror: no module named 'pip‘
@Resource和@Autowired的区别?
[crawler] avoid script detection when using selenium
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
[learn wechat from 0] [00] Course Overview
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
Static comprehensive experiment
MySQL导入SQL文件及常用命令
基于NeRF的三维内容生成
leetcode刷题:二叉树24(二叉树的最近公共祖先)
【从 0 开始学微服务】【00】课程概述
About IPSec
[pytorch practice] use pytorch to realize image style migration based on neural network
SQL Lab (41~45) (continuous update later)
leetcode刷题:二叉树22(二叉搜索树的最小绝对差)
Static routing assignment of network reachable and telent connections
如何将 @Transactional 事务注解运用到炉火纯青?
xshell评估期已过怎么办
利用栈来实现二进制转化为十进制