Artificial Intelligence Programming Lab(AIPLab) 討論區

Please login or register.

請輸入帳號, 密碼以及預計登入時間

新聞:

[慶賀]恭喜亞大獲《泰晤士報》亞洲最佳大學排名第83名,國內排名第十名-20170201

作者 主題: [php] 正規表示式  (閱讀 5873 次)

admin

  • 管理員
  • Hero Member
  • *****
  • 文章: 1897
    • 檢視個人資料
[php] 正規表示式
« 於: 三月 26, 2009, 12:26:49 pm »
程式碼: [Select]
<?php
// get host name from URL
preg_match('@^(?:http://)?([^/]+)@i'"http://www.php.net/index.html"$matches);
$host $matches[1];
// get last two segments of host name
preg_match('/[^.]+\.[^.]+$/'$host$matches);
echo 
"domain name is: {$matches[0]}\n";
?>

這是手冊上的例子,我看了很久而且在網路上找不到好的解釋,只好自己解釋
preg_match('@^(?:http://)?([^/]+)@i', "http://www.php.net/index.html", $matches);
  • (1) pattern 可以是"/ regex  /i" 或 '@ regex @i' ,後者的regex中,斜線/就只是斜線
  • (2) ^是指開頭的意思 $是結尾的意思, ^(http://)? 意思是開頭要是出現 http:// 零次或一次
  • (3) ?:是說不抓後面的http://到match的字串
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
  • (1) \.指的是 '.' dot 字元
  • (2) [^.]+是除了 '.' dot 字元之外的字元出現一次以上
  • (3) \.[^.]+$ 比對的字串在最後面是除了 '.' dot 字元之外的字元出現一次以上


參考PHP手冊
(?im) sets caseless, multiline matching.
/ab(?i)c/ matches only "abc" and "abC".
If an opening parenthesis is followed by "?:", the subpattern does not do any capturing

There are two different sets of meta-characters: those that are recognized anywhere in the pattern except within square brackets, and those that are recognized in square brackets. Outside square brackets, the meta-characters are as follows:

\ general escape character with several uses
^ assert start of subject (or line, in multiline mode)
$ assert end of subject (or line, in multiline mode)
. match any character except newline (by default)
[ start character class definition
] end character class definition
| start of alternative branch
( start subpattern
) end subpattern
? extends the meaning of (, also 0 or 1 quantifier, also quantifier minimizer
* 0 or more quantifier
+ 1 or more quantifier
{ start min/max quantifier
} end min/max quantifier

Part of a pattern that is in square brackets is called a "character class". In a character class the only meta-characters are:
\ general escape character
^ negate the class, but only if the first character
- indicates character range
] terminates the character class
 
已記錄
 

SimplePortal Classic 2.0.5