正则表达式学习笔记

在使用python的过程中,会遇到需要使用正则表达式的地方,比如:爬虫。但是一直都对正则表达式一知半解。

在学习shell脚本时,又再次遇到正则表达式相关的难题,决心将此工具攻克,下面为从0到1的学习心得记录:

参照博客:http://www.cnblogs.com/hanxiaoyu/p/5759477.html

书籍资料:shell脚本学习指南

环境:unix主机

开始:字符类:

实验:.匹配任意一个字符

[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e . #全匹配
On Friendship
And a youth said, "Speak to us of Friendship." 
Your friend is your needs answered. 
He is your field which you sow with love and reap with thanksgiving. 
And he is your board and your fireside. 
For you come to him with your hunger, and you seek him for peace. 
When your friend speaks his mind you fear not the "nay" in your own mind, nor
do you withhold the "ay." 
And when he is silent your heart ceases not to listen to his heart; 
For without words, in friendship, all thoughts, all desires, all expectations
are born and shared, with joy that is unacclaimed. 
When you part from your friend, you grieve not; 
For that which you love most in him may be clearer in his absence, as the
mountain to the climber is clearer from the plain. 
And let there be no purpose in friendship save the deepening of the spirit. 
For love that seeks aught but the disclosure of its own mystery is not love
but a net cast forth: and only the unprofitable is caught. 
And let your best be for your friend. 
If he must know the ebb of your tide, let him know its flood also. 
For what is your friend that you should seek him with hours to kill? 
Seek him always with hours to live. 
For it is his to fill your need, but not your emptiness. 
And in the sweetness of friendship let there be laughter, and sharing of
pleasures. 
For in the dew of little things the heart finds its morning and is refreshed.
[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e .our #匹配“our”带的字符
Your friend is your needs answered. 
He is your field which you sow with love and reap with thanksgiving. 
And he is your board and your fireside. 
For you come to him with your hunger, and you seek him for peace. 
When your friend speaks his mind you fear not the "nay" in your own mind, nor
And when he is silent your heart ceases not to listen to his heart; 
When you part from your friend, you grieve not; 
And let your best be for your friend. 
If he must know the ebb of your tide, let him know its flood also. 
For what is your friend that you should seek him with hours to kill? 
Seek him always with hours to live. 
For it is his to fill your need, but not your emptiness. 
[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e .firesi #匹配“firesi”的字符
And he is your board and your fireside. 

实验:[]

[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e [\"?z] #匹配出现了",?,z的字符,注意转意符\
And a youth said, "Speak to us of Friendship." 
When your friend speaks his mind you fear not the "nay" in your own mind, nor
do you withhold the "ay." 
For what is your friend that you should seek him with hours to kill? 
[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e [?]
For what is your friend that you should seek him with hours to kill? 
[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e ["?] #未添加转意符\,unix认为”后面年还有内容

> "


实验:-

[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e [1-9]
[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e [A-Z]
On Friendship
And a youth said, "Speak to us of Friendship." 
Your friend is your needs answered. 
He is your field which you sow with love and reap with thanksgiving. 
And he is your board and your fireside. 
For you come to him with your hunger, and you seek him for peace. 
When your friend speaks his mind you fear not the "nay" in your own mind, nor
And when he is silent your heart ceases not to listen to his heart; 
For without words, in friendship, all thoughts, all desires, all expectations
When you part from your friend, you grieve not; 
For that which you love most in him may be clearer in his absence, as the
And let there be no purpose in friendship save the deepening of the spirit. 
For love that seeks aught but the disclosure of its own mystery is not love
And let your best be for your friend. 
If he must know the ebb of your tide, let him know its flood also. 
For what is your friend that you should seek him with hours to kill? 
Seek him always with hours to live. 
For it is his to fill your need, but not your emptiness. 
And in the sweetness of friendship let there be laughter, and sharing of
For in the dew of little things the heart finds its morning and is refreshed.
[root@iZ23wdyiulaZ work]# cat 1.txt | grep -e [H-Z]
On Friendship
And a youth said, "Speak to us of Friendship." 
Your friend is your needs answered. 
He is your field which you sow with love and reap with thanksgiving. 
When your friend speaks his mind you fear not the "nay" in your own mind, nor
When you part from your friend, you grieve not; 
If he must know the ebb of your tide, let him know its flood also. 
Seek him always with hours to live. 


实验:^

实验:[[:digit:]]


数量:

位置限定符

位置限定符(Anchor):描述各种字符类以及普通字符之间的位置关系,例如邮件地址分三部分,用普通字符@和.隔 开,IP地址分四部分,用.隔开,每一部分都可以用

字符类和数量限定符 描述。为了表示位置关系,需要位置限定符的概念,将在下面介绍。





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值