linux 中正则表达式?、+、*、及.*的区别

 

001、?表示匹配0次或者1次

atcg?t 能够匹配 atct 或 atcgt,但是不能匹配 atcggt。

 

测试:

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
atct    888
atcggk  333
atcgt   444
atcgggkx        3333
[root@pc1 test]# awk '$1 ~ /atcg?t/' a.txt   ## g?表示匹配g0次或者匹配g1次
atct    888
atcgt   444

 

 

 

002、+ 表示匹配之前的项 1 次或多次

exon-9+能够匹配 exon-99 和 exon-9,但是不能匹配 exon-。

 

测试:

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
exon-99 888
exon-9  666
exon-   333
exon-88 777
[root@pc1 test]# awk '$1 ~ /exon-9+/' a.txt
exon-99 888
exon-9  666

 

 

003、*表示匹配之前的项 0 次或多次

exon-9*能够匹配 exon-*,但是不能匹配 exon。

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
exon-99 888
exon-9  666
exon-   333
exon-88 777
exon88  777
[root@pc1 test]# awk '$1 ~ /exon-9*/' a.txt   ## 9*表示匹配90次或者多次
exon-99 888
exon-9  666
exon-   333
exon-88 777

 

 

004、.* 表示匹配任意字符0次或者多次, 一般指匹配所有。

 

小结:

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
cl jjj
col kkk
coool mmm
cooool nnn
xxxx yyyy
[root@pc1 test]# awk '$1 ~ /co?l/' a.txt    ## ?表示匹配前一个字符0次或者多次
cl jjj
col kkk
[root@pc1 test]# cat a.txt
cl jjj
col kkk
coool mmm
cooool nnn
xxxx yyyy
[root@pc1 test]# awk '$1 ~ /co+l/' a.txt       ## +表示匹配前一个字符1次或者多次
col kkk
coool mmm
cooool nnn

 

 

[root@pc1 test]# ls
a.txt
[root@pc1 test]# cat a.txt
cl jjj
col kkk
coool mmm
cooool nnn
xxxx yyyy
[root@pc1 test]# awk '$1 ~ /co*l/' a.txt    ## *号表示匹配0次或者多次
cl jjj
col kkk
coool mmm
cooool nnn
[root@pc1 test]# cat a.txt
cl jjj
col kkk
coool mmm
cooool nnn
xxxx yyyy
[root@pc1 test]# awk '$1 ~ /co.*l/' a.txt    ## .*表示匹配任意字符0次或者多次
col kkk
coool mmm
cooool nnn

 

参考:https://mp.weixin.qq.com/s?__biz=MzU1NTk0MTUxMg==&mid=2247490679&idx=2&sn=9c2c050c02bb106839fab7ec72a2a0ae&chksm=fbcdf790ccba7e8659943e72198aa5a86ecac362841e19de519fe0bdefdf83708bb743203d46&mpshare=1&scene=23&srcid=01050wuMhaTBRKnW8dsga2B8&sharer_sharetime=1672894580394&sharer_shareid=50b75c6a886e09824b582fb782a7678b#rd

 

posted @ 2023-01-05 16:30  小鲨鱼2018  阅读(2073)  评论(0编辑  收藏  举报