2010-09-21 64 views
1

我需要從一個LDAP連接字符串,像這樣的提取信息:正則表達式匹配的LDAP連接字符串

ldap://uid=adminuser,dc=example,c=com:[email protected]/dc=basePath,dc=example,c=com 

我想用正則表達式來提取每個令牌對我來說,並放置在數組。我嘗試了一些正則表達式,我得到了最好的是這樣的:

/(\w+)\:\/\/(([a-zA-Z0-9]+)(:([^\:\@]+))[email protected])?([^\/]+)((\/[^#]*)?(#(.*))?)/ 

但不幸的是,當我在PHP執行它,我得到以下結果:

Array 
(
    [0] => ldap://uid=adminuser,dc=example,c=com:[email protected]/dc=basePath,dc=example,c=com 
    [1] => ldap 
    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => uid=adminuser,dc=example,c=com:[email protected] 
    [7] => /dc=basePath,dc=example,c=com 
    [8] => /dc=basePath,dc=example,c=com 
) 

任何人可以幫助我,讓我可以正確提取連接字符串的每個部分?

感謝

+1

哪種語言是您使用?你可以編輯你的問題來顯示你的樣本字符串的預期結果嗎?部件的順序和數量是否可變? – 2010-09-21 13:45:03

回答

5

([a-zA-Z0-9]+)顯得相當不足拿起含=和其他字符的字符串LDAP。

一般來說,你不應該試圖用正則表達式自己解析URL。相反,請使用標準庫函數parse_url()

這給:

array(5) { 
    ["scheme"]=> 
    string(4) "ldap" 
    ["host"]=> 
    string(16) "ldap.example.com" 
    ["user"]=> 
    string(30) "uid=adminuser,dc=example,c=com" 
    ["pass"]=> 
    string(6) "secret" 
    ["path"]=> 
    string(29) "/dc=basePath,dc=example,c=com" 
} 
+0

哇!這比正則表達式更好:)非常感謝! – MaxP 2010-09-21 17:23:08