2014-01-30 58 views

回答

5

這種模式'#^/abc/(?P<abc>[^/]++)$#si';分解像這樣:

/^\/abc\/(?P<abc>[^\/]++)$/si 

^ assert position at start of the string 
\/ matches the character/literally 
abc matches the characters abc literally (case insensitive) 
\/ matches the character/literally 
(?P<abc>[^\/]++) Named capturing group abc 
    [^\/]++ match a single character not present in the list below 
     Quantifier: Between one and unlimited times, as many times as possible, without giving back [possessive] 
     \/ matches the character/literally 
$ assert position at end of the string 
s modifier: single line. Dot matches newline characters 
i modifier: insensitive. Case insensitive match (ignores case of [a-zA-Z]) 

(來源:http://regex101.com/r/hS6qE3 - 確保你的網站上逃避你/,因爲它假定/是PHP定界符在你的模式。例如,#代替分隔符,然後用引號和字符串結束符;包裝。實際表達式僅爲^/abc/(?P<abc>[^/]++)$,包含單行和不區分大小寫的修飾符。)

注意使用兩個++標誌以及changes the behavior from greedy to possessive

字符串的一個例子,將匹配:

/abc/somethingelsenotafrontslash 

您可以閱讀簡單解釋有關preg_match_allhere