当前位置:网站首页>5 minutes to learn regular expressions
5 minutes to learn regular expressions
2022-06-26 05:45:00 【One mu of land in Shanghai】
The role of regular expressions :
Use one pattern String to match a similar segment in the target string . To put it bluntly , One day you see a string , You want to pull a piece out of it , You use regular expressions to match the paragraph you want
Write regular steps
1. First, fix the structure . Fixed things are written directly , Use parentheses instead of fixed or variable .
Case study 1:
For example, there is a kind of string
“admin-api-8xd5sdfa9p-asdf4”
“admin-api-qos3difd1c-2q5gh”
How to match such strings ?
First, fix the structure :”admin-api-()-()” , That's it . Keep writing directly , Use parentheses instead of parentheses , This is the structure .
Case study 2:
For example, find out from the string 11 Bit of mobile phone number
The target string is :” full name : Zhang Wei ; Telephone :13011348290”
We just want to match the phone number , Others don't . Phone number 11 Bits are not fixed .
Then the structure is a pair of parentheses “()” , That's it .
2. Find the regular characters you need in the quick look-up table below and fill them in the structure
Regular characters fall into two categories , One is content , A class that represents the number of occurrences .
such as . Belongs to the content character ,* The number character , that .* It means any string with any content and length . This is the most commonly used .
such as [0-9] Express 0-9 One of the numbers in , Is a content character ,{11} Appear 11 Time ,{10,20} Appear 10 Time to 20 Time , These are the number characters . therefore [0-9]{11} You can match the continuity 11 Digit number , That is, the telephone number .
So the complete regular expression of the above two cases is like this :
“admin-api-(\w{10})-(\w{5})”
“(\d{11})” perhaps “([0-9]{11})”
3.( the selected readings )Python General code for matching strings using regular expressions
import re # re yes python Function modules of the standard library , amount to java Medium Maven package
pattern = re.compile(“admin-api-(\w{
10})-(\w{
5})”) # Compile your regular expression , Fast execution
groups = pattern.match(“ Target string , such as admin-api-8xd5sdfa9p-asdf4”)
# groups It's a list , The first element is the longest string matched , Then the following elements are the matching contents of the first parenthesis 、 What the second parenthesis matches ……
# match The function is an ab initio match , If the string you want to match is not at the beginning of the target string , Then use groups = pattern.findall()
print(groups[0]) # Print the longest complete string you can match , This is supposed to be admin-api-8xd5sdfa9p-asdf4
print(groups[1]) # The string to which the first parenthesis matches , This is supposed to be 8xd5sdfa9p
Regular quick look-up table
In the structure () Represent the changing part , The following table also shows (?: ) (?=) (?!) Usage of . 



边栏推荐
- Unicloud cloud development obtains applet user openid
- 旧情书
- Something about MariaDB
- 【C語言】深度剖析數據在內存中的存儲
- 10 set
- Learn cache lines and pseudo sharing of JVM slowly
- Red team scoring method statistics
- Using Jenkins to perform testng+selenium+jsup automated tests and generate extendreport test reports
- Pytorch (environment, tensorboard, transforms, torchvision, dataloader)
- Daily production training report (17)
猜你喜欢

基于SDN的DDoS攻击缓解

【 langage c】 stockage des données d'analyse approfondie en mémoire

cartographer_ optimization_ problem_ 2d

cartographer_ fast_ correlative_ scan_ matcher_ 2D branch and bound rough matching

The difference between get and post in small interview questions

11 IO frame

Introduction to alluxio

【C語言】深度剖析數據在內存中的存儲

Redis discovery bloom filter

10 set
随机推荐
Summary of the 10th provincial Blue Bridge Cup
旧情书
kolla-ansible部署openstack yoga版本
Describe an experiment of Kali ARP in LAN
RIA ideas
12 multithreading
Use jedis to monitor redis stream to realize message queue function
怎么把平板作为电脑的第二扩展屏幕
1212312321
CMakeLists. txt Template
Some doubts about ARP deception experiment
The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
pytorch(网络模型)
The news of thunderbolt
【ARM】讯为rk3568开发板buildroot添加桌面应用
电机专用MCU芯片LCM32F037系列内容介绍
Win socket programming (Mengxin initial battle)
pytorch(网络模型训练)
Lesson 4 serial port and clock
Internship May 29, 2019