用于捕获键值逗号分隔值的正则表达式

Regex to capture key-value comma-separated values

我正在尝试编写一个正则表达式来解析 Unrealscript 序列化对象的值。其中一部分涉及这样的行:

(X=32.69,Y='123.321',Z="A string with commas, just to complicate things!",W=Class'Some.Class')

结果捕获应该是:

[
    {
        'X':32.69,
        'Y':'A string with commas, just to complicate things!',
        'Z':'Class\'Some.Class\'
    }
]

我想要的是能够区分键(例如X)和值(例如Class\'Some.Class\')。

这是我迄今为止尝试过的一种模式,只是为了捕获一组简单的值(目前暂时不尝试处理值内的逗号):

模式

\(((\S?)=(.+),?)+\)

数据集

(X=32,Y=3253,Z=12.21)

结果

https://regex101.com/r/gT9uU3/1

我仍然是这些正则表达式的新手,我们将不胜感激!

提前致谢。

您可以试试这个正则表达式来关联键值对:

(?!^\()([^=,]+)=([^[=10=]]+?)(?=,[^,]+=|\)$)

Regex live here.

解释:

(?!^\()         # do not match the initial '(' character

([^=,]+)        # to match the key .. we take all from the last comma
=               # till the next '=' character

([^[=11=]]+?)       # any combination '[^[=11=]]' - it will be the key's value
                  # at least one digit '+'
                  # but stops in the first occurrence '?'

(?=             # What occurrence?

    ,[^,]+=     # a comma ',' and a key '[^,]+='
                  # important: without the key:
                  # the occurrence will stop in the first comma
                  # that should or should not be the delimiter-comma 

    |\)$        # OR '|':  the value can also be the last one
                  # which has not another key in sequence,
                  # so, we must accept the value
                  # which ends '$' in ')' character

)               # it is all

希望对您有所帮助。

对不起我的英语,请随时编辑我的解释,或在评论中让我知道。 =)