一個Regex的看法(?:)

來源:互聯網
上載者:User

原正則的出現是為了實現apache上傳目錄無執行許可權,不過大家知道在 windows下對於檔案名稱是不區分大小寫,但在*unx下就不同了,其中st0p.php和st0p.Php是兩個不同的檔案,所以此正則(?i:.php)出現 複製代碼 代碼如下:<Directory "/var/www/upload">
<FilesMatch "(?i:.php)">
Order Allow,Deny
Deny from all
</FilesMatch>
</Directory>

kindle說是參考http://perldoc.perl.org/perlre.html#Extended-Patterns的內容進行寫的,具體的內容大家自己看,我們說一下關於此運算式的部分..

我們找到?:相關的部分,其用法有兩種(?:pattern)和(?imsx-imsx:pattern)
我們需要用的是後者,前者的話是沒法區分大小寫.而後者的用法就是(?標誌修飾符:格式)
其中原文中有一句話是Any letters between ? and : act as flags modifiers as with (?imsx-imsx).
這句話就說明了imsx-imsx的作用,標誌修飾符
我們看一下剛才出現的正則(?i:.php)
?的作用是在預設的貪婪模式下儘可能多的匹配所搜尋的字串,當該字元緊跟在任何一個其他限制符 (*, +, ?, {n}, {n,}, {n,m}) 後面時,匹配模式是非貪婪的。非貪婪模式儘可能少的匹配所搜尋的字串,例如,對於字串 "oooo",'o+?' 將匹配單個 "o",而 'o+' 將匹配所有 'o'。

其中i的作用為忽略大小寫
.php就是我們需要匹配的部分,由於?和i的作用,那我們的正則就會在不區分大小寫情況下儘可能多的匹配所搜尋的字串,這樣就達到了我們禁用所有.php尾碼檔案的訪問了

當然還有別的參數,更多的內容大家參考一下http://perldoc.perl.org/perlre.html#Extended-Patterns

m
Treat string as multiple lines. That is, change "^" and "$" from matching the start or end of the string to matching the start or end of any line anywhere within the string.

s
Treat string as single line. That is, change "." to match any character whatsoever, even a newline, which normally it would not match.
Used together, as /ms, they let the "." match any character whatsoever, while still allowing "^" and "$" to match, respectively, just after and just before newlines within the string.

i
Do case-insensitive pattern matching.
If use locale is in effect, the case map is taken from the current locale. See perllocale.

x
Extend your pattern's legibility by permitting whitespace and comments.

p
Preserve the string matched such that ${^PREMATCH}, ${^MATCH}, and ${^POSTMATCH} are available for use after matching.

g and c
Global matching, and keep the Current position after failed matching. Unlike i, m, s and x, these two flags affect the way the regex is used rather than the regex itself. See "Using regular expressions in Perl" in perlretut for further explanation of the g and c modifiers.

其中:應該只是一個分隔字元吧,不知道還有沒有別的作用,如果哪個大牛知道,請告訴我.

.php就是我們要匹配的部分了,從上面的圖我們..

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.