当前位置:网站首页>regular expression
regular expression
2022-06-21 17:36:00 【Xuanyuan】
List of articles
Regular expressions
regular expression
Regular expressions are a text pattern , Include normal characters ( for example ,a To z Between the letters ) And special characters ( be called " Metacharacters ").
Regular expressions are described using a single string 、 Match a string that matches a syntax rule .
**re Module USES
lookup
search # Only return one
match # Only return one , Only those that match from the beginning are counted
findall # Return string
finditer # return Match iterator
search,match All of them Match object
m.group() Display the data
m.group(1) Show first group
m.groups() Show all groups (1,2,3) Group
findall Return result array
Replace
sub # Replace
subn # After replacement , How much is replaced
re.sub(r'123','456',text)
Return the replaced string
re.subn(r'123','456',text)
Return the replaced string , And replaced a few places
Division
split
What segmentation is used for regular representation , Return the result list of segmentation
Example
text='sassas , sas ; sas'
re.split(r'\s*[,.;]\s*',text) # Multiple spaces [,.;] Multiple spaces
return ['sassas', 'sas', 'sas']
** Rule of grammar
Character category
| matching | Regular |
|---|---|
| a,b,c,1,2,3 | Constant , Write what is what |
| \d | A number |
| \D | A non numeric character |
| \s | A space |
| \S | A non space |
| \w | An arbitrary alphanumeric underline |
| [abc] | a,b,c Any character |
| [a-e] | a To e Any one of |
| [^a-d] | Take the opposite , except a,b,c,d Any one of |
| [\d] | Back space (Backspace) |
| . | Wildcard Division \n Any character outside |
quantifiers ( frequency )
| Regular | matching |
|---|---|
| * | 0 Or more |
| + | 1 Or more |
| ? | 0 or 1 individual |
| {2} | 2 individual |
| {2,5} | 2 To 5 |
| {2,} | least 2 |
| {,4} | most 4 |
Portfolio model
Multiple pattern combinations
\d{
6}[a-z]{
6} # Six numbers ,6 individual a-z
| # Both modes are OK , Such as two numbers or two characters \d{2}|\w{2}
# grouping , After grouping, you can use quantifiers in groups
() # Such as (122){3} matching 3 Time 122
Location
| Regular | matching |
|---|---|
| ^ | Start of string |
| \A | Start of string , Ignore m Mark |
| $ | End of string |
| \Z | End of string , Ignore m Mark |
| \b | At the word boundary |
| \B | Not at word boundaries |
| (?<=xxx)\d | matching xxx after Of A number |
| (?=xxx)\d | matching xxx The first number at the beginning |
| (?!XXX)\d | Matching is not xxx At the beginning A number |
grouping
It's too complicated to annotate
r'(sa)(?# What does the front match )'
| Regular | matching |
|---|---|
| (…) | A group |
| (?P) | Group name is Y Group |
| (?:…) | Do not capture groups |
| \Y | Matching first Y Matching groups |
| (?P=Y) | The matching name is Y Group |
| (?#) | notes |
Mark
re=re.findall(r'sad',flag=re.I)
| Regular | matching |
|---|---|
| i | Ignore case |
| m | ^ and $ Match the beginning and end of a line |
| s | . Match newline |
| x | Allow spaces and comments |
| \u | Unicode matching |
Replace
| Regular | matching |
|---|---|
| \g<0> | Insert the whole segment to match |
| \g | Insert matching Y,Y It can be a matching name or number |
| \Y | Insert, Y Group |
string matching
import re
# Fixed string
re.findall(r'123455',text)
r Express raw, Native string
# A certain category
re.findall(r'\d',text)
\d \d+
Decorate the preceding characters # Such as \d Match a number ,\d{4} Match four
# Combination match
re.findall(r'\d{2}abc\d{5}',text)
# Multiple situations ,
| The division , look for 123 or 456
re.findall(r'123|345')
# Limit position
^ Starting position
$ End position
# Internal constraints
w character ,() Means a group ,(\1) It means the same as the first group
text='abcabc5613111111'
re.findall(r'(\w{3})(\1)',text)
return [('abc','abc'),('111','111')]
Writing steps
1. Find matching characters
- Find the right model according to your needs
- Use the lookup function
2. Split string
- Observe the split pattern of the split string
- split function
3. Replace
- Find the right model according to your needs
- Use the substitution function
边栏推荐
猜你喜欢

Design a print function to print the whole tree

Qt5 knowledge: string list qstringlistmodel
![[MySQL learning notes 19] multi table query](/img/e6/4cfb8772e14bc82c4fdc9ad313e58a.png)
[MySQL learning notes 19] multi table query

《MATLAB 神经网络43个案例分析》:第27章 LVQ神经网络的预测——人脸朝向识别

Common formula of derivative__ Common formulas of indefinite integral

大话内存四区

应用架构原则

贝叶斯公式的两种理解

Accelerate the implementation of cloud native applications, and Yanrong yrcloudfile and Tianyi cloud have completed the Compatibility Certification

Variables and pointers
随机推荐
BM22 比较版本号
欧洲家具EN 597-1 跟EN 597-2两个阻燃标准一样吗?
线段树&树状数组模板
[MySQL learning notes 12] paging query
PTA L3-032 关于深度优先搜索和逆序对的题应该不会很难吧这件事 (30 分)
module. Exports points to problems
path. join() 、path. Basename() and path extname()
[MySQL learning notes 15] user management
稳若磐石的焱融 SaaS 服务平台背后,是数据生态的崛起
正则表达式
Detailed explanation of Fisher information quantity detection countermeasure sample code
FragmentStatePagerAdapter 与FragmentPagerAdapter的区别
In the "roll out" era of Chinese games, how can small and medium-sized manufacturers solve the problem of going to sea?
自然科学的根本任务
Serialization and deserialization of binary tree
[MySQL learning notes 13] comprehensive exercise of query statements
一些细节
From Beijing "moisten" to Chicago, engineer Baoyu "moisten" the secret of growth
变量与指针
The main relations and differences between Poisson sampling and Bernoulli sampling