当前位置:网站首页>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 .
边栏推荐
- 电商系统中常见的9大坑,你踩过没?
- matlab 使用 audiorecorder、recordblocking录制声音,play 播放声音,audiowrite 保存声音
- Selection of field types for creating tables in MySQL database
- MPLS experiment operation
- New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
- Cross domain? Homology? Understand what is cross domain at once
- Android high frequency network interview topic must know and be able to compare Android development environment
- JMeter (II) - install the custom thread groups plug-in
- ES6 new method of string
- Volume compression, decompression
猜你喜欢

开发那些事儿:如何利用Go单例模式保障流媒体高并发的安全性?

With the innovation and upgrading of development tools, Kunpeng promotes the "bamboo forest" growth of the computing industry
![[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing](/img/56/87bc8fca9ceeab6484f567f7231fdb.png)
[Video] visual interpretation of Markov chain principle and Mrs example of R language region conversion | data sharing
![[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

Raspberry pie 4B learning notes - IO communication (1-wire)

Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you

PR second training

人工智能在网络安全中的作用

Selection of field types for creating tables in MySQL database

Leetcode, 3 repeatless longest subsequence
随机推荐
【LeetCode 43】236. The nearest common ancestor of binary tree
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
Exclusive delivery of secret script move disassembly (the first time)
How to debug apps remotely and online?
MATLAB realizes voice signal resampling and normalization, and plays the comparison effect
Memorabilia of domestic database in June 2022
技术大佬准备就绪,话题C位由你决定
现货黄金分析的技巧有什么呢?
[Floyd] post disaster reconstruction
Basic concepts of machine learning
C language 3-7 daffodils (enhanced version)
Should enterprises choose server free computing?
How to use a product to promote "brand thrill"?
Finally got byte offer, 25-year-old inexperienced experience in software testing, to share with you
1217 supermarket coin processor
[rust web rokcet Series 1] Hello, world and get, post, put, delete
Ubuntu20.04 PostgreSQL 14 installation configuration record
Matlab uses audioread and sound to read and play WAV files
正则表达式学习笔记
6-2漏洞利用-ftp不可避免的问题