当前位置:网站首页>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
边栏推荐
- Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet
- Innovation and management based on Scrum
- Get together to watch (detailed version) eat a few cents a day
- Rhcsa second day operation
- MySQL JDBC programming
- Error installing Mongo service 'mongodb server' on win10 failed to start
- [suggestion collection] hematemesis sorting out golang interview dry goods 21 questions - hanging interviewer-1
- The penultimate K nodes in jz22 linked list
- windows MySQL8 zip安装
- Parental delegation model [understanding]
猜你喜欢
Deploying sbert model based on torchserve < semantic similarity task >
How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year
16、 Anomaly detection
[wechat applet] set the bottom menu (tabbar) for the applet
Fashion MNIST 数据集分类训练
Hack the box optimum
Yes, from today on, our fans can participate in Netease data analysis training camp for free!
Innovation and management based on Scrum
Flink stream processing engine system learning (III)
高效音乐格式转换工具Music Converter Pro
随机推荐
Solve the problem that the registered Google email Gmail mobile number cannot be used for verification
Suggestion: block reference sorting is in the order of keywords
1、 Sequence model
[XJTU計算機網絡安全與管理]第二講 密碼技術
Niuke hand speed monthly race 48 C (I can't understand the difference. It belongs to yes)
Learn regular expression options, assertions
Hack the box optimum
Six very 6 computer driver managers: what software is good for driver upgrade? Recommended by the best computer driver management software abroad
Log4j knowledge point record
Probabilistic model of machine learning
MySQL function syntax
MySQL JDBC programming
MySQL complex query uses temporary table / with as (similar to table variable)
Domestic lightweight Kanban scrum agile project management tool
SQL server2019 cannot download the required files, which may indicate that the version of the installer is no longer supported. What should I do
ROP Emporium x86_ 64 7 ~ 8 questions
MySQL复杂查询使用临时表/with as(类似表变量)
JZ35 replication of complex linked list
1215_ Hello world used by scons
Fashion MNIST dataset classification training