2017-03-26 32 views
0

我有一個短代碼。我想提取所有屬性及其值,並將它們放入數組中。正則表達式將短代碼解析爲數組

這是簡碼:

[res_map address="Yeronga QLD 4104, Australia" description="<img src='http://localhost/wordpress/wp-content/plugins/responsive-maps-plugin/includes/img/company.png'> {br} Yeronga QLD 4104, Australia {br} Phone: 0040 752 235 756" directionstext="(directions to our address)" icon="blue" iconsize="" style="2" scalecontrol="no" typecontrol="no" streetcontrol="no" locateme="no" zoom="13" zoomcontrol="no" draggable="yes" scrollwheel="no" searchbox="no" clustering="no" logging="no" poi="yes" fullscreen="no" width="100%" height="500px" maptype="roadmap" popup="no" center="" refresh="yes" key="AIzaSyDZ0ucX5SAiseQTn72Iw-An6fm0n2S71RA"] 

描述屬性中可以有任何HTML內容。我需要從這個簡碼中創建一個數組,如下所示:

address=>Yeronga QLD 4104, Australia 

description=><img src='http://local.....lia {br} Phone: 0040 752 235 756 

directionstext=>(directions to our address) 

icon=>blue 

等等其他屬性。

什麼是正確的正則表達式?

回答

0

以下解決方案僅適用,如果有一個字符串內沒有"

\s?\[?([^=]+)="([^"]*)"\]? 
  • \s?\[?:空格和[不應該被包含在一個關鍵,因此實際的小組賽之前匹配
  • ([^=]+)=:與每個字符最多匹配一個組=
  • "([^"]*)":匹配中的一個組
  • \]?:匹配的可選]

這是一個活生生的例子:https://regex101.com/r/DGaRCo/1

+0

有沒有「在字符串中。你很快:)解決方案的作品,非常感謝你! –