当前位置:网站首页>Regular expression learning notes
Regular expression learning notes
2022-07-02 01:49:00 【Stark Zhang Yu】
What is regular expression
Verify the validity of the data 、 Find the text that meets the requirements, and cut and replace the text .
The real power of regular expressions is that they can find text that conforms to a certain rule .
Metacharacters
1. Special string
- . Any character except newline
- \d Represents any single number
- \w Represents any single number or letter or underscore
- \s Represents any single blank character
\D \W \S, The meaning is just the opposite .
2. Blank character
- \r A carriage return
- \n A newline
- \f Page identifier
- \t tabs
- \v Vertical tabs
- \s Any whitespace
3. quantifiers
- * representative 0 Times to times
- + 1 Times to times
- ? 0 To 1 Time
- {m} appear m Time
- {m,} At least m Time
- {m,n} m To n Time
4. Range
- | or , Such as
ab|bcrepresentative ab or bc - […] A commonplace , Any single element in parentheses
- [a-z] matching a-z Any single element between
- [^…] Take the opposite , Cannot include any single element in parentheses
Greedy matching 、 Non greedy matching and exclusive mode
- The characteristic of greedy pattern is to match the maximum length as much as possible
- Non greedy patterns will match as short as possible .
- Exclusive mode and greedy mode are very similar , Exclusive patterns match as much as possible , If the match fails, it ends , No backtracking , This will save time . The specific method is to add a plus sign after the quantifier (+), for example
xy{1,3}+yz
Be careful : Python and Go None of the standard libraries currently support exclusive mode .
(“.+?”)|\w{2,6}
Grouping and referencing
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-uAMt23It-1656663240092)(https://note.youdao.com/yws/res/20143/WEBRESOURCEf8297d4f75fd7803bb138a4326c9a4fd)]
Regular as shown in the figure , Put the date and time in parentheses . There are two groups in this regular , Date is 1 individual , Time is the first 2 individual .
1. Don't save subgroups
The function of grouping is that it may be referenced later , But if you don't need references, you can add sub groups without saving , expression (?:xxx), The advantage is that the regular performance will be better , It is also less likely to make mistakes when counting subgroups .
2. Parentheses are nested
[ Failed to transfer the external chain picture , The origin station may have anti-theft chain mechanism , It is suggested to save the pictures and upload them directly (img-Euot1k2Y-1656663240095)(https://note.youdao.com/yws/res/20156/WEBRESOURCEe2aeab108bee9c5908c7e6405867abd8)]
Group references are used in lookups
… A little
Four matching patterns
1. Case insensitive mode (Case-Insensitive)
Case insensitive mode , Regular expressions (?i)cat, In this way, the matching characters are not case sensitive
If we want the result on the previous match , The case is the same as that of the second repetition , What should I do ? We just need to use parentheses to put modifiers and regular cat Part enclosed , Adding parentheses is equivalent to limiting the scope of action , Let case insensitivity only apply to the contents in this bracket . Regular expressions ((?i)cat)
2. Point number universal distribution mode (Dot All)
In the metacharacter . It can match any element except line feed , Use in single line mode ., Regular expressions (?s).
3. Multi line matching pattern
Expression of multiline pattern (?m)^...|...$, The advantage of this matching is that every line
4. Annotation mode
A little …
Assertion
Assertion refers to the requirement for the matching text location .
1. Word boundaries
When matching words accurately , We use \b...\b It can be realized , You can also specify the beginning and end of the expression ^...$
2. Look around
Looking around is mainly about clearly defined boundaries .
(?<=Y), On the left is Y(?<=!Y), The left is not Y(?=Y), On the right is Y(?!Y), The right side is not Y
The left angle bracket stands for looking to the left , There is no angle bracket. Look to the right , Exclamation mark means right and wrong .
边栏推荐
- There are spaces in the for loop variable in the shell -- IFS variable
- Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
- 遷移雲計算工作負載的四個基本策略
- How to use a product to promote "brand thrill"?
- Raspberry pie 4B learning notes - IO communication (1-wire)
- Discussion on the idea of platform construction
- 技术大佬准备就绪,话题C位由你决定
- VARIATIONAL IMAGE COMPRESSION WITH A SCALE HYPERPRIOR文献实验复现
- MySQL view concept, create view, view, modify view, delete view
- SQLite 3 of embedded database
猜你喜欢
![[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing](/img/ba/dcb276768b1a9cc84099f093677d29.png)
[Video] Markov chain Monte Carlo method MCMC principle and R language implementation | data sharing

6-2 vulnerability exploitation - inevitable problems of FTP

机器学习基本概念

6-2漏洞利用-ftp不可避免的问题

MPLS experiment operation

Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months

Self drawing of menu items and CListBox items

This is the report that leaders like! Learn dynamic visual charts, promotion and salary increase are indispensable

Android: how can golden nine and silver ten squeeze into the first-line big factories from small and medium-sized enterprises? The depth of interview questions in large factories

Implementation of Weibo system based on SSM
随机推荐
How to use a product to promote "brand thrill"?
NeRV: Neural Reflectance and Visibility Fields for Relighting and View Synthesis
[技术发展-21]:网络与通信技术的应用与发展快速概览-1- 互联网网络技术
[Floyd] post disaster reconstruction
三分钟学会基础k线图知识
The concepts and differences between MySQL stored procedures and stored functions, as well as how to create them, the role of delimiter, the viewing, modification, deletion of stored procedures and fu
D discard the virtual recovery method
Using mongodb in laravel
KS006基于SSM实现学生成绩管理系统
Exclusive delivery of secret script move disassembly (the first time)
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
[Obsidian] wechat is sent to Obsidian using remotely save S3 compatibility
城市选择器组件实现原理
321. Chessboard segmentation (2D interval DP)
479. Additive binary tree (interval DP on the tree)
734. Energy stone (greed, backpack)
Four basic strategies for migrating cloud computing workloads
Three core problems of concurrent programming
Laravel artisan 常用命令
Electronic Society C language level 1 32, calculate the power of 2