当前位置:网站首页>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
边栏推荐
- Looking for a job for 4 months, interviewing 15 companies and getting 3 offers
- At the end of the year, I'll teach you how to get high performance!
- July training (the 26th day) - and check the collection
- Nacos configuration center dynamically refreshes the data source
- PCL的ICP配准示例
- 刷题《剑指Offer》day03
- 超赞的卡尔曼滤波详解文章
- DCGAN论文改进之处+简化代码
- 3D修复论文:Shape Inpainting using 3D Generative Adversarial Network and Recurrent Convolutional Networks
- 直播倒计时 3 天|SOFAChannel#29 基于 P2P 的文件和镜像加速系统 Dragonfly
猜你喜欢

华为交换机双上行组网Smart-link配置指南

Understand chisel language. 24. Chisel sequential circuit (IV) -- detailed explanation of chisel memory

NFT系统开发-教程

35 spark streaming backpressure mechanism, spark data skew solution and kylin's brief introduction

Snowflake vs. Databricks谁更胜一筹?2022年最新战报

圆环工件毛刺(凸起)缺口(凹陷)检测案例

XML overview

交换机端口镜像配置指南

Food safety | is sugar free really sugar free? These truths need to be known

Leetcode.814. binary tree pruning____ DFS
随机推荐
c'mon! Please don't ask me about ribbon's architecture principle during the interview
中高级试题」:MVCC 实现原理是什么?
食品安全 | 垃圾食品越吃越想吃?这份常见食品热量表请收好
[SCM]源码管理 - perforce 分支的锁定
Write yourself a year-end summary. Happy New Year!
ACL2021最佳论文出炉,来自字节跳动
July training (day 14) - stack
QT learning (II) -- a brief introduction to QT Creator
Exercises --- quick arrangement, merging, floating point number dichotomy
【云原生 • DevOps】一文掌握容器管理工具 Rancher
圆环工件毛刺(凸起)缺口(凹陷)检测案例
活体检测综述
吃透Chisel语言.25.Chisel进阶之输入信号处理(一)——异步输入与去抖动
7/26 思维+dp+后缀数组的学习
Provincial Emergency Management Department: Guangzhou can strive to promote the experience of emergency safety education for children
刷题《剑指Offer》day03
3D人脸重建:Joint 3D Face Reconstruction and Dense Alignment with position Map Regression Network
Food safety | the more you eat junk food, the more you want to eat it? Please keep this common food calorimeter
找工作 4 个月, 面试 15 家,拿到 3 个 offer
Food safety | are you still eating fermented rice noodles? Be careful these foods are poisonous!