如何解析给定的键值参数字符串?

How to parse a given key-value parameters string?

我有这个字符串:

Parameter1="Something related to this" Parameter2="Another value" Parameter3='Single quotes are permitted' Parameter4="Even HTML entities are permitted"

我想得到这个列表:

  1. Parameter1=与此相关的东西
  2. 参数2=另一个值
  3. 参数3=允许单引号
  4. 参数4=允许甚至HTML个实体

我试过这个正则表达式。但它不起作用:

(\w+)=(('|"|")).*(('|"|"))

如何解析这个字符串并提取键值对?

您可以使用

(\w+)=(['"]|")(.*?)

参见regex demo详情:

  • (\w+) - 第 1 组:一个或多个单词字符
  • = - 等号
  • (['"]|") - 第 2 组:'""
  • (.*?) - 第 3 组:除 LF 字符外的任何零个或多个字符尽可能少
  • - 第 2 组值。