Regular expression (extract key/value pairs)

半腔热情 提交于 2021-01-28 18:33:31

问题


I'm trying to extract a list (match) of key/value pairs from a string. Ex :

PATH_1:"/", PATH_2:"/OtherPath", TODAY:"2016-06-27",XYZ :"1234"

This should give :

      Key            Value
      PATH_1         /
      PATH_2         /OtherPath
      TODAY          2016-06-27
      XYZ            1234

Here is what I have so far as regex :

((?:"[^"]*"|[^:,])*):((?:"[^"]*"|[^:,])*)

This is well working except that when I'm adding a path having a '\'. Ex :

PATH_1:"c:\", PATH_2:"c:\OtherPath", TODAY:"2016-06-27" 

I don't know how to instruct to regex expression to jump over semi-colon when found inside double quote sequence. Hope someone can help me.

PS : I'm using QT.

Best regards,


回答1:


https://regex101.com/r/vB1rS1/2

It seems that just removing the : from the last [] may do it if the quotes are being removed.

((?:"[^"]*"|[^:,])*):((?:"[^"]*"|[^,])*)


来源:https://stackoverflow.com/questions/38057771/regular-expression-extract-key-value-pairs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!