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

时间:2015-09-25 23:33:48

标签: regex unrealscript

我试图编写一个正则表达式来解析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

我仍然是这些正则表达式的新手,任何帮助都将不胜感激!

提前致谢。

1 个答案:

答案 0 :(得分:1)

您可以尝试使用此正则表达式来关联键和值对:

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

Regex live here.

解释

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

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

([^\0]+?)       # any combination '[^\0]' - 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

希望它有所帮助。

对不起我的英文,请随时编辑我的解释,或在评论中告诉我。 =)