1

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

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

提前致谢。

4

1 回答 1

2

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

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

正则表达式住在这里。

解释:

(?!^\()         # 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

希望能帮助到你。

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

于 2015-09-26T00:29:11.807 回答