有人可以提供正则表达式来解析字符串中的名称/值对吗?这些对用逗号分隔,值可以用引号括起来.例如:
AssemblyName=foo.dll,ClassName="SomeClass",Parameters="Some,Parameters"
Markus Jarde.. 35
无处可逃:
/([^=,]*)=("[^"]*"|[^,"]*)/
键和值的双引号转义:
/((?:"[^"]*"|[^=,])*)=((?:"[^"]*"|[^=,])*)/ key=value,"key with "" in it"="value with "" in it",key=value" "with" "spaces
反斜杠字符串转义:
/([^=,]*)=("(?:\\.|[^"\\]+)*"|[^,"]*)/ key=value,key="value",key="val\"ue"
完全反斜杠逃脱:
/((?:\\.|[^=,]+)*)=("(?:\\.|[^"\\]+)*"|(?:\\.|[^,"\\]+)*)/ key=value,key="value",key="val\"ue",ke\,y=val\,ue
编辑:添加了转义替代品.
Edit2:添加了另一个转义替代方案.
您必须通过删除任何转义字符和周围的引号来清理键/值.
无处可逃:
/([^=,]*)=("[^"]*"|[^,"]*)/
键和值的双引号转义:
/((?:"[^"]*"|[^=,])*)=((?:"[^"]*"|[^=,])*)/ key=value,"key with "" in it"="value with "" in it",key=value" "with" "spaces
反斜杠字符串转义:
/([^=,]*)=("(?:\\.|[^"\\]+)*"|[^,"]*)/ key=value,key="value",key="val\"ue"
完全反斜杠逃脱:
/((?:\\.|[^=,]+)*)=("(?:\\.|[^"\\]+)*"|(?:\\.|[^,"\\]+)*)/ key=value,key="value",key="val\"ue",ke\,y=val\,ue
编辑:添加了转义替代品.
Edit2:添加了另一个转义替代方案.
您必须通过删除任何转义字符和周围的引号来清理键/值.