当前位置:网站首页>Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
Introduction to regular expressions of shell, general matching, special characters: ^, $,., * Character range (brackets): [], special characters: \, matching mobile phone number
2022-07-27 10:00:00 【Redamancy06】
List of articles
1. Getting started with regular expressions
Regular expressions are described using a single string 、 Match a series of strings that conform to a certain syntax rule . In many text editors , Regular expressions are often used for retrieval 、 Replace the text that matches a pattern . stay Linux in ,grep,sed,awk And other text processing tools support pattern matching through regular expressions .
1.1 Regular match
A string of regular expressions without special characters matches itself , for example :
[[email protected] ~]# cat /etc/passwd | grep 00:
It will match all that contain 00: The line of .
1.2 Special characters are often used
1.2.1 Special characters :^
^ Match the beginning of a line , for example :
It will match all of them with a Beginning line
1.2.2 Special characters :$
$ Match the end of a line , for example 
It will match all of them with h The line at the end
- reflection :^$ Match what ?
Match blank line

daily_archive.sh File is in http://t.csdn.cn/0Wpml Written , If you want to see it, click here to see the contents
1.2.3 Special characters :.
Match an arbitrary character , for example 
1.2.4 Special characters :*
“*” Not used alone , He used it with the last character , Means to match the previous character 0 Times or times , for example
[[email protected] ~]# cat /etc/passwd | grep ro*t
Will match rt, rot, root, rooot, roooot Wait for all the lines
reflection :.* Match what ?
Match any character , You can talk to ^$ Continuous use , Put it among them , You can implement the following code , With a start ,login The line at the end 
[[email protected] ~]# cat /etc/passwd | grep ^a.*login$

[[email protected] ~]# cat /etc/passwd | grep ^a.*var.*login$
1.2.5 Character range ( brackets ):[ ]
[ ] Means to match a character in a range , for example
[6,8]------ matching 6 perhaps 8
[0-9]------ Match one 0-9 The number of
[0-9]------ Matches any length of numeric string
[a-z]------ Match one a-z Characters between
[a-z] ------ Matches an alphabetic string of any length
[a-c, e-f]- matching a-c perhaps e-f Any character between

[[email protected] ~]# cat /etc/passwd | grep r[a-z]*t
1.2.6 Special characters :\
\ To signify an escape , It will not be used alone . Because all special characters have their own matching patterns , When we want to match a particular character itself ( for example , I want to find out all the things that contain ‘$’ The line of ), There will be difficulties . At this point, we need to use escape characters with special characters , To represent the special character itself , for example

It will match all that contain $ The line of . Note that you need to use single quotes to cause the expression to .
1.2.7 Match cell phone number

[[email protected] scripts]# echo “13812345678” | grep ^1[34578][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]$

[[email protected] scripts]# echo “13812345678” | grep -E ^1[34578][0-9]{9}$
Remember to capitalize -E, Because at present grep I won't support it {9} Extended regular , So we need to add -E, Express regular expressions that support extensions
边栏推荐
- S switch stacking scheme configuration guide
- After one year, the paper was finally accepted by the international summit
- 2016 outlook
- Gbase 8A MPP cluster capacity expansion practice
- 食品安全 | 还在吃酵米面吗?当心这些食物有毒!
- 7/26 thinking +dp+ suffix array learning
- July training (day 14) - stack
- July training (day 18) - tree
- 原生input标签的文件上传
- 35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍
猜你喜欢

数据分析如何解决商业问题?这里有份超详细攻略

Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children

吃透Chisel语言.24.Chisel时序电路(四)——Chisel内存(Memory)详解

吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)
![[scm] source code management - lock of perforce branch](/img/c6/daead474a64a9a3c86dd140c097be0.jpg)
[scm] source code management - lock of perforce branch

Review of in vivo detection

Why do microservices have to have API gateways?

How to install cpolar intranet penetration on raspberry pie

NFT系统开发-教程

Talk about 10 scenarios of index failure. It's too stupid
随机推荐
吃透Chisel语言.27.Chisel进阶之有限状态机(一)——基本有限状态机(Moore机)
深度剖析分库分表最强辅助Sharding Sphere
July training (day 11) - matrix
Anchor Free检测器:CenterNet
Why do microservices have to have API gateways?
Snowflake vs. Databricks谁更胜一筹?2022年最新战报
Understand chisel language. 22. Chisel sequential circuit (II) -- detailed explanation of chisel counter: counter, timer and pulse width modulation
Expose a technology boss from a poor family
Towards the peak of life
蚂蚁集团境外站点 Seata 实践与探索
Shell中的文本处理工具、cut [选项参数] filename 说明:默认分隔符是制表符、awk [选项参数] ‘/pattern1/{action1}filename 、awk 的内置变量
Case of burr (bulge) notch (depression) detection of circular workpiece
July training (day 16) - queue
Food safety | the more you eat junk food, the more you want to eat it? Please keep this common food calorimeter
Understand chisel language. 26. Chisel advanced input signal processing (II) -- majority voter filtering, function abstraction and asynchronous reset
35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction
I grabbed a ticket and thought I found the system bug of 12306
At the end of the year, I'll teach you how to get high performance!
DCGAN论文改进之处+简化代码
Final examination paper of engineering materials