当前位置:网站首页>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 . 



边栏推荐
- Wechat team sharing: technical decryption behind wechat's 100 million daily real-time audio and video chats
- Internship May 29, 2019
- [activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
- cartographer_ optimization_ problem_ 2d
- The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
- String类学习
- FindControl的源代码
- 旧情书
- one billion two hundred and twelve million three hundred and twelve thousand three hundred and twenty-one
- [arm] build boa based embedded web server on nuc977
猜你喜欢

How Navicat reuses the current connection information to another computer

Learn cache lines and pseudo sharing of JVM slowly

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

Henkel database custom operator '~~‘

Leetcode114. Expand binary tree into linked list

kolla-ansible部署openstack yoga版本

bingc(继承)

DOM document

String类学习
![[upsampling method opencv interpolation]](/img/6b/5e8f3c2844f0cbbbf03022e0efd5f0.png)
[upsampling method opencv interpolation]
随机推荐
RIA想法
Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
睛天霹雳的消息
[arm] add desktop application for buildreoot of rk3568 development board
ZigBee learning in simple terms Lecture 1
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]
Redis discovery bloom filter
Sql查询时间段内容
Consul服务注册与发现
Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal
Something about MariaDB
REUSE_ ALV_ GRID_ Display event implementation (data_changed)
The State Council issued a document to improve the application of identity authentication and electronic seals, and strengthen the construction of Digital Government
Sofa weekly | open source person - Yu Yu, QA this week, contributor this week
组合模式、透明方式和安全方式
1212312321
[activity recommendation] cloud native, industrial Internet, low code, Web3, metauniverse... Which is the architecture hot spot in 2022
Project suspension
工厂方法模式、抽象工厂模式
适配器模式