2017-03-26 30 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

有没有“在字符串中。你很快:)解决方案的作品,非常感谢你! –