当前位置:网站首页>Shell script learning notes - regular expressions
Shell script learning notes - regular expressions
2022-04-23 02:47:00 【XL's Princess】
One 、 The regular expression is in shell There are only commands in the grep、sed、awk The command supports regular expressions .
Two 、 Special characters
| Locator | explain |
|---|---|
| ^ | Anchor the beginning ^a With a start The default anchor is one character |
| $ | Anchor end a$ With a ending The default anchor is one character |
Test cases :
# Exactly match , With a start , With c ending ,"^ac$" It's a match ac
[root@localhost shell]# egrep "^ac$" zzbds.sh
ac
# Fuzzy matching
[root@localhost shell]# egrep "^a" zzbds.sh
ac
ab
abbc
abcc
aabbcc
abbbc
abbbbbc
acc
abc
asb
aa
a_c
aZc
aAAAAc
a c
ababababab
[root@localhost shell]# egrep "c$" zzbds.sh
ac
abbc
abcc
aabbcc
abbbc
abbbbbc
acc
abc
a_c
aZc
aAAAAc
a c
ccc
[root@localhost shell]#
Match symbol . Match string
| Match symbol | explain |
|---|---|
| . | Match any character except carriage return |
| () | String grouping |
| [ ^ ] | Indicates the character in the character class that appears in negative brackets , Take the opposite |
| \ | Escape character |
| Pipe, | or |
Test cases
[root@localhost shell]# egrep "^a.c$" zzbds.sh
acc
abc
a_c
aZc
a c
[root@localhost shell]# egrep "^a[0-9]c$" zzbds.sh
a3c
[root@localhost shell]# egrep "^a[^0-9]c$" zzbds.sh
acc
abc
a_c
aZc
[root@localhost shell]# egrep "^a\*c$" zzbds.sh
a*c
[root@localhost shell]# egrep "^(a|b)c$" zzbds.sh
ac
# With a The beginning or b start
[root@localhost shell]# egrep "^(a|b)c$" zzbds.sh
ac
bc
qualifiers : Make a qualified description of the preceding character or string
| qualifiers | explain |
|---|---|
| * | Add... After a character * Indicates that the character does not appear or appears more than once |
| ? | Similar to the asterisk , But it means that the character appears once or does not appear |
| + | Indicates that the character before it appears one or more times , But there must be one |
| {n,m} | Appears after a character , Indicates that the character is at least n Time , most m Time |
| {m} | Just happened to appear m Time |
Test case
[root@localhost shell]# egrep "^ab*c$" zzbds.sh
ac
abbc
abbbc
abbbbbc
abc
[root@localhost shell]# egrep "^ab?c$" zzbds.sh
ac
abc
[root@localhost shell]# egrep "^ab+c$" zzbds.sh
abbc
abbbc
abbbbbc
abc
[root@localhost shell]# egrep "^ab{2,4}c$" zzbds.sh
abbc
abbbc
[root@localhost shell]# egrep "^ab{2,5}c$" zzbds.sh
abbc
abbbc
abbbbbc
[root@localhost shell]# egrep "^ab{3}c$" zzbds.sh
abbbc
3、 ... and 、POSIX Special characters
| Special characters | explain |
|---|---|
| [:alnum:] | Match any alphabetic character 0-9 a-z A-Z |
| [:alpha:] | Match any letter , Uppercase or lowercase |
| [:digit:] | Numbers 0-9 |
| [:graph:] | Non empty character ( Non whitespace control characters ) |
| [:lower: ] | Lowercase characters a-z |
| [:upper: ] | Uppercase characters A-Z |
| [:cntrl:] | Control characters |
| [:print:] | Non empty character ( Including Spaces ) |
| [:punct: | Punctuation |
| [:blank:] | Space and TAB character |
| [:xdigit:] | 16 Hexadecimal Numbers |
| [:space:] | All blank characters ( New line 、 Space 、 tabs ) |
Test cases
[root@localhost shell]# egrep "^a[[:alnum:]]$" zzbds.sh
ac
ab
aa
[root@localhost shell]# egrep "^a[[:alnum:]]c$" zzbds.sh
acc
abc
aZc
a3c
[root@localhost shell]# egrep "^a.c$" zzbds.sh
acc
abc
a_c
aZc
a3c
a*c
[root@localhost shell]# egrep "^a[[:upper:]]c$" zzbds.sh
aZc
[root@localhost shell]# egrep "^a[[:lower:]]*c$" zzbds.sh
ac
abbc
abcc
aabbcc
abbbc
abbbbbc
acc
abc
# Match valid ip Address ,
[root@localhost shell]# egrep '^((25[0-5]|2[0-4][[:digit:]]|[01]?[[:digit:]][[:digit:]]?).){3}(25[0-5] |2[0-4][[:digit:]]|[01]?[[:digit:]][[:digit:]]?)$' --color ip.sh
1.1.1.1
192.168.11.0
# Match with 250.-255. perhaps 200.-249. perhaps 0.-99. At the beginning , And previous rules have appeared 3 Time , And the character at the end of the previous rule
'^((25[0-5]|2[0-4][[:digit:]]|[01]?[[:digit:]][[:digit:]]?).){3} (25[0-5] |2[0-4][[:digit:]]|[01]?[[:digit:]][[:digit:]]?)$')
> Match phone number
> egrep "^[[:graph:]]{12}$" numder |grep "^(0[1-9][0-9][0-9]?)-[1-9][0-9]{6,7}$"
Four 、 Expand


for example :
1、
e: Play chicken game , Play games tonight
If only filter : Play chicken game
You need to : play .*? game
.*? Represents as few matches as possible
2、
Specifies the character partition
e:123x456x789x
result :
123x
456x
789x
You need to :.*?x
Reference resources :
Regular expressions Don't to character string The beginning and the end ( Pre check before and after ):https://blog.csdn.net/q965844841qq/article/details/111368696?
matching “<a href=” And “" class="ulink"” All characters in the middle (?<=<a href=").+(?=" class="ulink")
Online regular expression testing :https://tool.oschina.net/regex
版权声明
本文为[XL's Princess]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220741454870.html
边栏推荐
猜你喜欢

Specific field information of MySQL export table (detailed operation of Navicat client)

How to build an integrated industrial Internet plus hazardous safety production management platform?

Hack the box optimum

windows MySQL8 zip安装

JVM运行时数据区(一)

Target narak

国产轻量级看板式Scrum敏捷项目管理工具
![[wechat applet] set the bottom menu (tabbar) for the applet](/img/e2/98711dfb1350599cbdbdf13508b84f.png)
[wechat applet] set the bottom menu (tabbar) for the applet

C语言 171. 最近回文数

Slave should be able to synchronize with the master in tests/integration/replication-psync. tcl
随机推荐
leetcode 烹飪料理
Web learning record (medium)
[XJTU計算機網絡安全與管理]第二講 密碼技術
字符串去掉空格问题
Les derniers noeuds K de la liste jz22
leetcode 烹饪料理
B blocks of the 46th ICPC Asian regional competition (Kunming)
[hcip] detailed explanation of six LSAS commonly used by OSPF
Decision tree principle of machine learning
下载正版Origin Pro 2022 教程 及 如何 激 活
Suggestion: block reference sorting is in the order of keywords
@Usage and difference between mapper and @ repository
Log4j knowledge point record
机器学习(周志华) 第十四章概率图模型
Codeforces Round #784 (Div. 4) (A - H)题解
The express project changes the jade template to art template
Flink learning (XI) watermark
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
Solve the problem that PowerShell mining occupies 100% of cpu7 in win7
JVM class loader