Artificial Intelligence Programming Lab(AIPLab) 討論區

Please login or register.

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



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


  • 管理員
  • Hero Member
  • *****
  • 文章: 1897
    • 檢視個人資料
[php] 正規表示式
« 於: 三月 26, 2009, 12:26:49 pm »
程式碼: [Select]
// get host name from URL
$host $matches[1];
// get last two segments of host name
"domain name is: {$matches[0]}\n";

preg_match('@^(?:http://)?([^/]+)@i', "", $matches);
  • (1) pattern 可以是"/ regex  /i" 或 '@ regex @i' ,後者的regex中,斜線/就只是斜線
  • (2) ^是指開頭的意思 $是結尾的意思, ^(http://)? 意思是開頭要是出現 http:// 零次或一次
  • (3) ?:是說不抓後面的http://到match的字串
preg_match('/[^.]+\.[^.]+$/', $host, $matches);
  • (1) \.指的是 '.' dot 字元
  • (2) [^.]+是除了 '.' dot 字元之外的字元出現一次以上
  • (3) \.[^.]+$ 比對的字串在最後面是除了 '.' dot 字元之外的字元出現一次以上

(?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