正則表達式練習(實驗樓會員課程)

[寫在前面的話:本人實驗樓會員爲大家提供省錢的兌換碼輸入我的邀請碼加入實驗樓會員,可享受30元優惠!邀請碼:2454DJL。3000多個編程實驗在線實踐,開始學習:https://www.shiyanlou.com/vip]

1:正則表達式介紹與練習

1.1 實驗內容

本次實驗將介紹正則表達式的一些基本概念和用法。

1.2 實驗知識點

正則表達式的概念
特殊符號

1.3 實驗環境

課程使用的實驗環境爲 Ubuntu Linux 14.04 64 位。實驗中會用到程序:

Xfce終端

1.4:什麼是正則表達式

在做文字處理或編寫程序時,用到查找、替換等功能,使用正則表達式能夠簡單快捷的完成目標。簡單而言,正則表達式通過一些特殊符號的幫助,使用戶可以輕鬆快捷的完成查找、刪除、替換等處理程序。例如 grep, expr, sed , awk. 或 Vi 中經常會使用到正則表達式,爲了充分發揮 shell 編程的威力,需要精通正則表達式。正規表示法基本上是一種『表示法』, 只要工具程序支持這種表示法,那麼該工具程序就可以用來作爲正規表示法的字符串處理之用。 也就是說,例如 vi, grep, awk ,sed 等等工具,因爲她們有支持正規表示法, 所以,這些工具就可以使用正規表示法的特殊字符來進行字符串的處理。

1.5:首先是正則表達式的特殊符號:

[:alnum:]代表英文大小寫字母及數字 
[:alpha:]代表英文大小寫字母
[:blank:]代表空格和 tab 鍵
[:cntrl:]鍵盤上的控制按鍵,如 CR,LF,TAB,DEL
[:digit:]代表數字
[:graph:]代表空白字符以外的其他
[:lower:]小寫字母
[:print:]可以被打印出來的任何字符
[:punct:]代表標點符號
[:upper:]代表大寫字符
[:space:]任何會產生空白的字符如空格,tab,CR 等
[:xdigit:]代表 16 進位的數字類型

1.5.1 特殊符號實例

任意建立一個文本並編輯。或者以原有的文本做實驗皆可。

$  touch regular_express.txt
$  vi regular_express.txt 
 //或者  sudo gedit regular_express.txt

將文本內容粘貼進regular_express.txt文件

"Open Source" is a good mechanism to develop programs.
apple is my favorite food.
Football game is not use feet only.
this dress doesn't fit me.
However, this dress is about $ 3183 dollars.
GNU is free air not free beer.
Her hair is very beauty.
I can't finish the test.
Oh! The soup taste good.
motorcycle is cheap than car.
This window is clear.
the symbol '*' is represented as start.
Oh!My god!
The gd software is a library for drafting programs.
You are the best is mean you are the no. 1.
The world <Happy> is the same with "glad".
I like dog.
google is the best tools for search keyword.
goooooogle yes!
go! go! Let's go.
# I am VBird

查找小寫字母

$ grep -n '[[:lower:]]' regular_express.txt 

操作截圖(藍色爲匹配成功的字符)

ycsong

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章