pattern match 樣式比對(PERL)
m// (match operator) 搜尋符合Regular Expression 的字串或陳述式.
m/PATTERN/
; "/"=>分界元delimiter ,if use "/" => "m" can be abridged.
=> ex : if($text !~ /exit/i){}
can use other delimiter ex: m()、m[]、m{}、m<> , but "m" can't be abridged .
*pattern modifier 樣式修正元
=>一序列的"選項",放置在 regular expression last delimiter(分界元)之後.改變搜尋方法與解析方式.
/i => 忽略大小寫
/m => 從"行"開頭開始比對
^ => 從"字串"開頭開始比對 m/^/
/o
/s
/x
/g =>傳回所有符合的比對,而不只是第一個.
s/// (substitution operator)以新字串取代符合Regular Expression 的字串或陳述式.
s/PATTERN/REPLACEMENT/
tr/// (translation operator)只能作用在純量,或陣列與雜湊內的單一元素.
tr/SEARCHLIST/REPLACEMENTLIST/
delimiter of SEARCHLSIT can be not the same as one of REPLACEMENTLIST.
ex. $string =~ tr(:)[/];
/c =>SEARCHLIST的補集合
/d =>刪除 CHAR NOT IN REPLACEMENTLIST
/s =>重複的字元縮成一個
ex1.從鍵盤內讀入輸入文字,如果不論大小寫的"exit"輸入,則結束.
print "Enter a text:\n";
while(<>){
if(m/exit/i){exit;}
print "Enter a text : \n";
}
ex2.傳回所有符合四個字元的單字串列.
符合運算子的傳回值 = 符合運算子傳回取代的字串.
@a = ("There are many cats." =~ m/\b\w{4,4}\b/g);
print "@a";
// many cats
/b=>單字邊界
/w=>尋找一個英數字元
{4,4}=>最少4個字元,最多4個字元
ex3.以dog 取代 cat
$text = "I have a dog.";
$text =~ s/dog/cat/;
print $text;
ex4 取代運算子的傳回值 = 取代運算子傳回取代的數目.
$string =" TEXT is the uppercase of text";
$num = $string =~ s{text}{book}gi; //($num = $string) =~ s{text}{book}gi => will be $string value , not return value.
print $num; //2
print $string; //book is the uppercase of book
ex5.將字串$string 內所有的小寫字元改成大寫.
$string = "Basketball is my favorite sport.";
$string =~ tr/a-z/A-Z/;
print $string;
//BASKETBALL IS MY FAVORTIE SPORT.
ex6.將$text的 所有非字母(a-z A-Z)字元,以"-"來取代.
$string = "Basketball is my favorite sport."
$string =~ tr/a-zA-Z/-/c;
print $string;
//Basketball-is-my-favorite-sport
ex7.將 $string 內的 "\n" 字元刪除
$string = "Basketball \n is \n my \n favorite \n sport\n.";
$string =~ tr/\n//d;
print $string;
//Basketball is my favorite sport.
ex8.將重複的字元歸為單一個.
$string = "good book ball baa seek haaaa";
$string =~ tr/a-z/a-z/s;
print $string;
// god bok bal ba sek ha
ex9.計算$string 非英數字元數目.
$string = "Basketball is my favorite sport.";
$num = $string =~ tr/a-zA-Z0-9//cs;
print $string;
print $num;
//
ex10.\w符合一個字元,\;脫逸出".";\w+符合一個單字
$string = "This is a house.";
$string =~ s/\w+/There/g;
print $string;
// There There There There
ex11. 取代第一個符合的 英數字元開頭的字串
$text = "Java language is the best.";
$text =~ s/[A-Za-z]+/PERL/;
print $text;
//Perl language is the best.
"^": 反集合
$text =~ s/[^A-Za-z]+/PERL/;
print $text;
//JavaPerllanguage is the best.
Personal notes ~
2008年2月18日 星期一
訂閱:
張貼留言 (Atom)
追蹤網誌清單
IT Info
Reference
標籤
- 代討論
- 防盜
- 常規表示式(regular expression)
- 進度
- 電子地圖
- 網站搬家程式
- Ajax Library
- Application of Perl
- C#
- CakePHP
- Coding Convention
- CSS
- Data injection
- DBI
- Debug
- Diff Browser
- Effect : Slideshow
- Engineering - Desing Pattern
- framework
- Framework - CodeIgniter
- Functions
- Imgick
- jquery
- JS Framework
- JS Library
- JS Object : Statement
- Linux
- Mapping Other Language
- Need to Turnning
- NetBeans
- OOAD-MVC
- open source suit software
- Performance
- Perl Installation
- PHP Class
- PHP rpc Java
- PHP to PERL
- Presentation
- RIA
- Serculity
- Service
- SQL injection
- Tips
- Tools
- Variable
- Versus
- WCF
- WebSite Thumbnail
- XML
- ZK Ajax
沒有留言:
張貼留言