藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月26日 11:18


請教:正則的限制範圍的顯示?

[code]

[root@localhost _shell_200603]# more shuzi
1........5
5........10
10.......20
100.......200
see section 2.3
dfd dffdfdf fdfdf 4.5.7 5345
[root@localhost _shell_200603]# grep '[0-9][0-9]*\.\.*[0-9][0-9]*' shuzi
1........5
5........10
10.......20
100.......200
see section 2.3
dfd dffdfdf fdfdf 4.5.7 5345

[root@localhost _shell_200603]# sed 's/\([0-9][0-9]*\)\.\{5,\}\([0-9][0-9]*\)/\1-\2/' shuzi
1-5
5-10
10-20
100-200
see section 2.3
dfd dffdfdf fdfdf 4.5.7 5345
[/code]

為什麼也輸出帶字母的行呢?
"."的範圍也限制了呀?

帶字母的行也匹配你的正則表達式

sed 's/\([0-9][0-9]*\)\.\{5,\}\([0-9][0-9]*\)/\1-\2/' shuzi

這個裡面沒有字母的呀...

-->

也沒指定不匹配字母

[^a-zA-Z]

只要句子中包含"\([0-9][0-9]*\)\.\{5,\}\([0-9][0-9]*\)"就打印出來

SED默認是打印全部輸出,如果要打印指定行應用n,p參數。所以這裡就是:

sed -n 's/\([0-9][0-9]*\)\.\{5,\}\([0-9][0-9]*\)/\1-\2/p' shuzi

-->




謝謝qbqqq和AT!



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com