【问题标题】:Parsing string with Lua table structure用 Lua 表结构解析字符串
【发布时间】:2018-10-09 11:44:22
【问题描述】:

我有一个具有这种结构的字符串(它是 Lua 表)

{['Name1']={score=235,x=6,y=88},['Name2']={score=112,x=12,y=55},['Name3']={score=15,x=15,y=56},}

为了便于阅读:

{['Name1']={score=235,x=6,y=88},
['Name2']={score=112,x=12,y=55},
['Name3']={score=15,x=15,y=56},} <--there might be more entries than these three of course

我需要为每个名称找到值 Name1、score、x 和 y。

我相信正则表达式可以做到,但我不擅长使用它。因此,如果有经验丰富的人给我一些模式,我会很高兴。

【问题讨论】:

  • 使用/\[\'(\w+)\'\]={score=(\d+),x=(\d+),y=(\d+)/

标签: php regex parsing


【解决方案1】:

您可以使用this one等在线工具测试您的正则表达式

您也可以让您更轻松,不使用正则表达式并通过此子字符串 "},[" 拆分字符串,您将获得一些带有子字符串的值数组:

0 -> {['Name1']={score=235,x=6,y=88 
1 -> 'Name2']={score=112,x=12,y=55
2 -> 'Name3']={score=15,x=15,y=56},}

然后你也可以用 "]={" 分割每个条目,你将拥有

0.0 -> {['Name1'
0.1 -> score=235,x=6,y=88 
1.0 -> 'Name2'
1.1 -> score=112,x=12,y=55
2.0 -> 'Name3'
2,1 -> score=15,x=15,y=56},}

...等等...

这不是一个很好的解决方案,但它可以帮助您在不使用正则表达式的情况下完成任务。

【讨论】:

    猜你喜欢
    • 2020-02-03
    • 1970-01-01
    • 2022-01-11
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 2014-08-26
    • 2018-10-20
    • 1970-01-01
    相关资源
    最近更新 更多