当前位置:网站首页>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 .
边栏推荐
- 如何用一款产品推动「品牌的惊险一跃」?
- 人工智能在网络安全中的作用
- Altium designer measure distance (ctrl+m)
- Another programmer "deleted the library and ran away", deleted the code of the retail platform, and was sentenced to 10 months
- Android: the kotlin language uses grendao3, a cross platform app development framework
- 企业应该选择无服务器计算吗?
- What are the affordable Bluetooth headsets? Student party parity Bluetooth headset recommendation
- Failed to transform file 'xxx' to match attributes
- Design and implementation of radio energy transmission system
- 跨域?同源?一次搞懂什么是跨域
猜你喜欢

Learning note 3 -- Key Technologies of high-precision map (Part 1)

Leetcode, 3 repeatless longest subsequence

k线图形态这样记(口诀篇)

Learn C language from scratch day 025 (maze)

The concept, function, characteristics, creation and deletion of MySQL constraints

Six lessons to be learned for the successful implementation of edge coding
![[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

Should enterprises choose server free computing?

迁移云计算工作负载的四个基本策略

Pyldavis installation and use | attributeerror: module 'pyldavis' has no attribute' gensim '| visual results are exported as separate web pages
随机推荐
The technology boss is ready, and the topic of position C is up to you
Ubuntu20.04 PostgreSQL 14 installation configuration record
Design and implementation of radio energy transmission system
New news, Wuhan Yangluo international port, filled with black technology, refreshes your understanding of the port
MySQL如何解决delete大量数据后空间不释放的问题
JMeter (I) - download, installation and plug-in management
Automatically browse pinduoduo products
Edge computing accelerates live video scenes: clearer, smoother, and more real-time
Architecture evolution from MVC to DDD
np.where 和 torch.where 用法
Basic concepts of machine learning
Réseau neuronal convolutif (y compris le Code et l'illustration correspondante)
2022 Q2 - 提升技能的技巧总结
ES6 new method of string
Convolutional neural network (including code and corresponding diagram)
Develop those things: how to use go singleton mode to ensure the security of high concurrency of streaming media?
Matlab uses audiorecorder and recordblocking to record sound, play to play sound, and audiobook to save sound
matlab 使用 resample 完成重采样
电子协会 C语言 1级 32、计算2的幂
Memorabilia of domestic database in June 2022