使用正则表达式模式在字符串中查找单词lua

我有一个作为字符串的 rest URL --> rest/dashboard/person/hari/categrory/savingaccount/type/withdraw

在这个 URL 中,我需要获取 person 和 category 之间的值以及 categrory 和 type 之间的值,因为这些值将动态变化

rest/dashboard/person/{{}}/categrory/{{}}/type/withdraw

我尝试使用 string.gsub(mystring, "([%w]+%/)([%w%d]+)"),但似乎这不是正确的方法

请帮忙

点赞
用户107090
用户107090

string.match 带有捕获的能力,是这个任务所需要的正确工具。 尝试运行以下代码:

s="rest/dashboard/person/hari/category/savingaccount/type/withdraw"
print(s:match("/person/(.-)/category/(.-)/type/"))
2018-08-13 16:02:35
用户8291949
用户8291949

你可以像建议的那样使用懒惰点 . -,或使用否定字符类 [^/]+

s = "rest/dashboard/person/hari/category/savingaccount/type/withdraw"
print(s:match("person/([^/]+)/category/"))
print(s:match("category/([^/]+)/type/"))

演示

2018-08-13 16:09:30