当前位置:网站首页>Regular expression summary
Regular expression summary
2022-07-02 05:51:00 【Zebra!】
Catalog
1. Introduction to regular expressions
2.6 Selection and backreference
3 Modifiers and metacharacters
1. Introduction to regular expressions
Regular expressions (Regular Expression) It's 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 .
Why use regular expressions ?
Typical search and replace operations require you to provide exact text that matches the expected search results . Although this technique may be sufficient for simple search and replace tasks on static text , But it lacks flexibility , If you use this method to search dynamic text , Even if it's not impossible , At least it will be very difficult .
Test the pattern within the string
replace text
Extract substrings from strings for pattern matching
2. regular expression syntax
2.1 Ordinary character
Normal characters include all printable and nonprintable characters that are not explicitly specified as metacharacters . This includes all uppercase and lowercase letters 、 All figures 、 All punctuation and some other symbols .
[A-Z] Represents an interval , Match all capital letters ,[a-z] Means all lowercase letters .
[...] Represents a match [...] All characters in , for example [aeiou] Match string "google runoob taobao" All of the e o u a Letter .
Match except [^...] All the characters of the characters in , for example [^aeiou] Match string "google runoob taobao" In addition to e o u a All the letters of the letter .
.: Match break (\n、\r) Any single character other than , Equivalent to [^\n\r].
[\s\S]: Match all .\s Is to match all blanks , Including line breaks ,\S Not blank , Not including line breaks .
\w: Match the letter 、 Numbers 、 Underline . Equivalent to [A-Za-z0-9_]
2.2 Print character
Nonprinting characters can also be part of regular expressions . The following table lists escape sequences that represent non print characters
2.3 Special characters
Special characters , Just some characters with special meanings
2.4 qualifiers
Qualifiers are used to specify how many times a given component of a regular expression must appear to satisfy a match . Yes * or + or ? or {n} or {n,} or {n,m} common 6 Kind of .
By means of *、+ or ? Place after qualifier ?, The expression is from " greedy " The expression is converted to " Not greed " Expression or minimum match .
2.5 Locator
^: Matches where the input string starts ;
$: Matches the position of the end of the input string ;
\b: Matches a word boundary , That is, the position between words and spaces ;
\B: Non word boundary matching
2.6 Selection and backreference
Use parentheses () Enclose all the options , Use... Between adjacent options | Separate .
Adding parentheses to both sides of a regular expression pattern or part of a pattern will cause related matches to be stored in a temporary buffer , Each sub match captured is stored in the order that it appears from left to right in the regular expression pattern
3 Modifiers and metacharacters
3.1 Modifier
Modifier | meaning | describe |
i | ignore - Case insensitive | Set matching to case insensitive , Search is case insensitive : A and a There is no difference between . |
g | global - The global matching | Find all the matches . |
m | multi line - Multi-line matching | Make the boundary character ^ and $ Match the beginning and end of each line , Remember it's multiple lines , Instead of the beginning and end of the entire string . |
s | Special character dot . Include line breaks in \n | Dots by default . It's match division newline \n Any character other than , add s After the modifier , . Include line breaks in \n. |
3.2 Metacharacters
+: Match previous subexpression one or more times ;
\d: Matches a numeric character . Equivalent to [0-9].
\D: Matches a non-numeric character . Equivalent to [^0-9].
wait , There are still a lot of it
3.3 priority
Regular expressions are evaluated from left to right , And follow the order of precedence
4 Matching rules
Everything starts from the most basic . Pattern , Is the most basic element of a regular expression , They are a set of characters that describe the characteristics of a string . The pattern can be simple , Composed of ordinary strings , It can also be very complicated , Special characters are often used to represent characters in a range 、 Recurring , Or context .
About regular expressions in python Please refer to my other blog :
python Based on learning -- Strings and regular expressions
Reference resources :https://www.runoob.com/regexp/regexp-tutorial.html
边栏推荐
- Zzuli:1064 encrypted characters
- 460. LFU cache bidirectional linked list
- Online music player app
- 深度学习分类网络--VGGNet
- 页面打印插件print.js
- Thunder on the ground! Another domestic 5g chip comes out: surpass Huawei and lead the world in performance?
- Several keywords in C language
- Pytorch Chinese document
- [leetcode] day92 container with the most water
- Cambrian was reduced by Paleozoic venture capital and Zhike shengxun: a total of more than 700million cash
猜你喜欢
PHP development and testing WebService (soap) -win
Importation de studio visuel
5g market trend in 2020
《CGNF: CONDITIONAL GRAPH NEURAL FIELDS》阅读笔记
如何写出好代码 — 防御式编程指南
Brew install * failed, solution
Software testing learning - day 4
[personal test] copy and paste code between VirtualBox virtual machine and local
深度学习分类网络--VGGNet
Grbl software: basic knowledge of simple explanation
随机推荐
Visual Studio导入
[leetcode] day92 container with the most water
1036 Boys vs Girls
Minimum value ruler method for the length of continuous subsequences whose sum is not less than s
[personal test] copy and paste code between VirtualBox virtual machine and local
软件测试答疑篇
php内的addChild()、addAttribute()函数
Cube magique infini "simple"
[PHP是否安装了 SOAP 扩]对于php实现soap代理的一个常见问题:Class ‘SoapClient‘ not found in PHP的处理方法
Usage record of vector
Zzuli:1069 learn from classmate Z
TypeScript的泛型和泛型约束
Résumé de la collection de plug - ins couramment utilisée dans les outils de développement idea
495.提莫攻击
Software testing learning - day 4
Web页面用户分步操作引导插件driver.js
Lantern Festival gift - plant vs zombie game (realized by Matlab)
Zzuli:1067 faulty odometer
Zzuli: maximum Convention and minimum common multiple
Reading notes of cgnf: conditional graph neural fields