简体   繁体   English

匹配无或多个Lua模式

[英]matching none or more Lua pattern

I am wondering if it was possible to make a pattern that could work like this: 我想知道是否有可能制作一个可以这样工作的模式:

With [icon=star w=32 h=32 spin=90] , it would return a table with: 使用[icon=star w=32 h=32 spin=90] ,它将返回一个表格:

icon: star  
w: 32  
h: 32  
spin: 90

I've tried doing [icon=(.-) ((.-)=(.-))] but it breaks. 我已经尝试过[icon=(.-) ((.-)=(.-))]但它会中断。

Lua doesn't have regex in the literal sense. Lua在字面意义上没有正则表达式。 It uses patterns. 它使用模式。

So, for your case, I'd much rather use gsub(or gmatch): 所以,对于你的情况,我宁愿使用gsub(或gmatch):

local str = "[icon=star w=32 h=32 spin=90]"
local tR = {}
str:gsub( "(%w+)%=(%w+)", function( x, y ) tR[x] = y end )

And your tR will have the exact result you wanted. 并且您的tR将获得您想要的确切结果。


More tutorials on gmatch and gsub are: 有关gmatch和gsub的更多教程是:

Using the following expression: (\\w+(?=\\=))=((?<=\\=)\\w+) group 1 of each match would be the left hand side and group 2 of each match would be the right hand side. 使用以下表达式: (\\w+(?=\\=))=((?<=\\=)\\w+)每个匹配的组1将是左侧,每个匹配的组2将是右侧。

Example: http://regexr.com?3478b 示例: http//regexr.com?3478b

Try this: 尝试这个:

(\w+)=(\w+)

Each match will have two groups: 每场比赛将有两组:

  • Group 1 will be the "name" 第1组将是“名称”
  • Group 2 will be the "value" 第2组将是“价值”
for k, v in ("[icon=star w=32 h=32 spin=90]"):gmatch("(%w+)=(%w+)")
  do print(k..":",v) end

icon:   star
w:  32
h:  32
spin:   90

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

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