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



边栏推荐
- Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
- The use of loops in SQL syntax
- Feelings of virtual project failure
- Leetcode114. Expand binary tree into linked list
- Lesson 4 serial port and clock
- 【 langage c】 stockage des données d'analyse approfondie en mémoire
- 新的征程
- A new journey
- Supplementary course on basic knowledge of IM development (II): how to design a server-side storage architecture for a large number of image files?
- Positioning setting horizontal and vertical center (multiple methods)
猜你喜欢

Could not get unknown property ‘*‘ for SigningConfig container of type org.gradle.api.internal

重载和重写

Uni app ceiling fixed style

Learn cache lines and pseudo sharing of JVM slowly

Henkel database custom operator '~~‘

适配器模式

cartographer_ optimization_ problem_ 2d

Leetcode114. Expand binary tree into linked list
![Operator priority, associativity, and whether to control the evaluation order [detailed explanation]](/img/c3/a646a7c7cb82e00746923f7b023058.jpg)
Operator priority, associativity, and whether to control the evaluation order [detailed explanation]

【ARM】在NUC977上搭建基于boa的嵌入式web服务器
随机推荐
ZigBee learning in simple terms Lecture 1
The model defined (modified) in pytoch loads some required pre training model parameters and freezes them
uniCloud云开发获取小程序用户openid
力扣 875. 爱吃香蕉的珂珂
Posting - don't get lost in the ocean of Technology
About XXX management system (version C)
Pytorch中自己所定义(修改)的模型加载所需部分预训练模型参数并冻结
Mise en file d'attente des messages en utilisant jedis Listening redis stream
Consul service registration and discovery
Talk 5 wireless communication
cross entropy loss = log softmax + nll loss
原型模式,咩咩乱叫
国务院发文,完善身份认证、电子印章等应用,加强数字政府建设
5分钟包你学会正则表达式
[arm] build boa based embedded web server on nuc977
BOM文档
BOM document
LeetCode_二叉搜索树_简单_108.将有序数组转换为二叉搜索树
Pre-Sale Analysis
使用Jenkins执行TestNg+Selenium+Jsoup自动化测试和生成ExtentReport测试报告