简体   繁体   English

用于标记化由逗号分隔的Key = Value对的列表的正则表达式,其中Value本身可能包含逗号

[英]Regular Expression for tokenizing the list of Key=Value pair seperated by comma(,), where Value itself may contain a comma(,) in it

I have a requirement where I need to tokenise the list of key=value pairs separated by comma. 我有一个需要标记用逗号分隔的键=值对的列表的要求。 Ex: $val1=abc, $val2=cde, $val3=efg 例如: $val1=abc, $val2=cde, $val3=efg

Expected Output: 6 tokens 预期输出:6个令牌

  1. $val1 $ val1
  2. abc abc
  3. $val2 $ val2
  4. cde cde
  5. $val3 $ val3
  6. efg efg

I am using the regex ([^\\" =,]*(\\"[^\\"]*\\")[^\\" =,]*)|[^\\" =,]+ which solves the above problem. 我正在使用正则表达式([^\\" =,]*(\\"[^\\"]*\\")[^\\" =,]*)|[^\\" =,]+解决了上述问题。 But it is not handling the case when the value of key= value pair has a comma in it. 但它不处理的情况下,当键= 对的中有一个逗号。

Ex: $val1=abc,AB, $val2=cde, $val3=efg 例如: $val1=abc,AB, $val2=cde, $val3=efg

Expected Output: 6 tokens 预期输出:6个令牌

  1. $val1 $ val1
  2. abc,AB abc,AB
  3. $val2 $ val2
  4. cde cde
  5. $val3 $ val3
  6. efg efg

But with the above regex am getting the output as shown below: Output: 7 tokens 但是使用上述正则表达式时,输出如下所示:输出:7个令牌

  1. $val1 $ val1
  2. abc abc
  3. AB AB
  4. $val2 $ val2
  5. cde cde
  6. $val3 $ val3
  7. efg efg

Can someone provide a proper regular expression which fits for the above case. 有人可以提供适合上述情况的正确正则表达式吗?

[^= ]+(?! \$)

See it in action 实际观看

Reads any amount of characters, that are not = or space and are not followed by a space and $ . 读取任意数量的字符,这些字符不是=或空格,并且后面没有空格和$

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM