2

我想从 Lua 中的字符串中解析一些数据。我尝试了几种组合,string.matchstring.sub没有运气。这是详细信息...

str='[{id:78749,name:Hrithik Roshan,character:Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,name:Priyanka Chopra,character:Kaali Gawde,ord'

fixstr = string.gsub(str,"name:","<actors>")
fixstr = string.gsub(fixstr,"character:","<actors>")
print(fixstr)
fixstr1 = string.match( fixstr, "<actors>(.+)<actors>")
print(fixstr1)

输出:

的输出 rint(fixstr)

[{id:78749,<actors>Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,<actors>Kaali Gawde,ord

的输出 print(fixstr1)

Hrithik Roshan,<actors>Vijay Deenanath Chauhan,order:0,cast_id:1,profile_path:/1uGhDRNCA9I4WvyD9TfgKYnLEhZ.jpg},{id:77234,<actors>Priyanka Chopra,

我想要做的是得到所有的字符串,<actors>...<actors> 但它没有奏效。有人可以帮忙吗?

4

1 回答 1

3

要获取 and 之间的所有字符串<actors>,请<actors>使用string.gmatch全局匹配:

for name in string.gmatch(fixstr, "<actors>(.-)<actors>") do
    print(name)
end

请注意使用.-in which-匹配零次或多次出现,但它是非贪婪的。

输出:

Hrithik Roshan,
Priyanka Chopra,

实际上,除非您需要fixstr其他用途,否则您不需要替换name:and character:<actors>只需运行一次string.gmatch即可:

for name in string.gmatch(str, "name:(.-)character:") do
    print(name)
end
于 2013-12-12T05:30:45.130 回答