大家都尝试了所有的字符串pattrens和库参数,但仍然卡住了。
我想从以下字符串中获取导演的名字我已经尝试过string.matcH但是它匹配来自字符串的第一个字符
字符串是......
fixstrdirector = {id:39254,cast:[{id:15250,姓名:Hope Davis,角色:姨妈Debra,订单:5,cast_id:10,profile_path:/aIHF11Ss8P0A8JUfiWf8OHPVhOs.jpg},{id:53650,名字:Anthony Mackie,角色:Finn,顺序:3,cast_id:11,profile_path:/5VGGJ0Co8SC94iiedWb2o3C36T.jpg},{id:19034,姓名:Evangeline Lilly,角色:Bailey Tallet,顺序:2,cast_id:12,profile_path:/ oAOpJKgKEdW49jXrjvUcPcEQJb3.jpg},{id:6968,姓名:Hugh Jackman,角色:Charlie Kenton,顺序:0,cast_id:13,profile_path:/wnl7esRbP3paALKn4bCr0k8qaFu.jpg},{id:79072,姓名:Kevin Durand,角色:Ricky, order:4,cast_id:14,profile_path:/c95tTUjx5T0D0ROqTcINojpH6nB.jpg},{id:234479,name:Dakota Goyo,character:Max Kenton,order:1,cast_id:15,profile_path:/7PU6n4fhDuFwuwcYVyRNVEZE7ct.jpg},{id: 8986,姓名:James Rebhorn,角色:Marvin,顺序:6,cast_id:16,profile_path:/ezETMv0YM0Rg6YhKpu4vHuIY37D.jpg},{id:930729,姓名:Marco Ruggeri,角色:Cliff,顺序:7,cast_id:17,profile_path :/1Ox63ukTd2yfOf1LVJOMXwmeQjO.jpg},{id:19860,姓名:Karl Yune,角色:Tak Mashido,orde r:8,cast_id:18,profile_path:/qK315vPObCNdywdRN66971FtFez.jpg},{id:111206,姓名:Olga Fonda,角色:Farra Lemkova,顺序:9,cast_id:19,profile_path:/j1qabOHf3Pf82f1lFpUmdF5XvSp.jpg},{id: 53176,姓名:John Gatins,角色:Kingpin,顺序:10,cast_id:41,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:1126350,姓名:Sophie Levy,角色:大姐,顺序:11,cast_id:42, profile_path:null},{id:1126351,姓名:Tess Levy,角色:Little Sister,顺序:12,cast_id:43,profile_path:null},{id:1126352,姓名:Charlie Levy,角色:Littlest姐妹,顺序: 13,cast_id:44,profile_path:null},{id:187983,name:Gregory Sims,character:Bill Panner,order:14,cast_id:45,profile_path:null}],crew:[{id:58726,name: Leslie Bohem,department:写作,职业:剧本,profile_path:null},{id:53176,姓名:John Gatins,department:写作,职业:编剧,profile_path:/ A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:17825,姓名:Shawn Levy,department:Directing,job:Director,profile_path:/7f2f8EXdlWsPYN0HPGcIlG21xU.jpg},{id:12415,姓名:Richard Matheson,部门:写作,工作:故事,profile_path:null},{id:57113,姓名:Dan Gilroy,部门:写作,工作:故事,profile_path:null},{id:25210,姓名:Jeremy Leven,部门:写作,工作:故事,profile_path:null},{id:17825,姓名:Shawn Levy,部门:生产,工作:生产者,profile_path:/7f2f8EXdlWsPYN0HPGcIlG21xU.jpg},{id:34970,姓名:Susan Montford,部门:生产,工作:制片人,profile_path:/1XJt51Y9ciPhkHrAYE0j6Jsmgji.jpg},{id:3183,姓名:Don Murphy,部门:生产,工作:生产者,profile_path:null},{id:34967,姓名:Rick Benattar,部门:生产,工作:制片人, profile_path:null},{id:1126348,name:Eric Hedayat,department:Production,job:Producer,profile_path:null},{id:186721,name:Ron Ames,department:Production,job:Producer,profile_path:null} ,{id:10956,姓名:Josh McLaglen,部门:生产,工作:执行制片人,profile_path:null},{id:57634,姓名:Mary McLaglen,部门:制作,工作:执行制片人,profile_path:null},{ id:23779,姓名:Jack Rapke,部门:生产,工作:执行制片人,profile_path :null},{id:488,姓名:Steven Spielberg,部门:生产,工作:执行制片人,profile_path:/cuIYdFbEe89PHpoiOS9tmo84ED2.jpg},{id:30,姓名:Steve Starkey,部门:制作,工作:执行制片人, profile_path:null},{id:24,姓名:Robert Zemeckis,部门:生产,工作:执行制片人,profile_path:/isCuZ9PWIOyXzdf3ihodXzjIumL.jpg},{id:531,姓名:Danny Elfman,部门:声音,工作:原创音乐Composer,profile_path:/pWacZpYPos8io22nEiim7d3wp2j.jpg},{id:18265,姓名:Mauro Fiore,部门:船员,工作:Cinematography,profile_path:null},{id:54271,姓名:Dean Zimmerman,部门:编辑,职位:编辑,profile_path:null},{id:25365,name:Richard Hicks,department:Production,job:Casting,profile_path:null},{id:5490,name:David Rubin,department:Production,job:Casting,profile_path:null },{id:52088,姓名:Tom Meyer,department:Art,job:Production Design,profile_path:null}]}
我试过过string.match(fixstrdirector,“name:(。+),department:Directing”)
但它让我从第一次发现它找到了thr字符串结尾的名称
输出:
Hope Davis,character:Aunt Debra,order:5,cast_id:10,profile_path:/aIHF11Ss8P0A8JUfiWf8OHPVhOs.jpg},{id:53650,name:Anthony Mackie,character:Finn,order:3,cast_id:11,profile_path:/5VGGJ0Co8SC94iiedWb2o3C36T.jpg},{id:19034,name:Evangeline Lilly,character:Bailey Tallet,order:2,cast_id:12,profile_path:/oAOpJKgKEdW49jXrjvUcPcEQJb3.jpg},{id:6968,name:Hugh Jackman,character:Charlie Kenton,order:0,cast_id:13,profile_path:/wnl7esRbP3paALKn4bCr0k8qaFu.jpg},{id:79072,name:Kevin Durand,character:Ricky,order:4,cast_id:14,profile_path:/c95tTUjx5T0D0ROqTcINojpH6nB.jpg},{id:234479,name:Dakota Goyo,character:Max Kenton,order:1,cast_id:15,profile_path:/7PU6n4fhDuFwuwcYVyRNVEZE7ct.jpg},{id:8986,name:James Rebhorn,character:Marvin,order:6,cast_id:16,profile_path:/ezETMv0YM0Rg6YhKpu4vHuIY37D.jpg},{id:930729,name:Marco Ruggeri,character:Cliff,order:7,cast_id:17,profile_path:/1Ox63ukTd2yfOf1LVJOMXwmeQjO.jpg},{id:19860,name:Karl Yune,character:Tak Mashido,order:8,cast_id:18,profile_path:/qK315vPObCNdywdRN66971FtFez.jpg},{id:111206,name:Olga Fonda,character:Farra Lemkova,order:9,cast_id:19,profile_path:/j1qabOHf3Pf82f1lFpUmdF5XvSp.jpg},{id:53176,name:John Gatins,character:Kingpin,order:10,cast_id:41,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:1126350,name:Sophie Levy,character:Big Sister,order:11,cast_id:42,profile_path:null},{id:1126351,name:Tess Levy,character:Little Sister,order:12,cast_id:43,profile_path:null},{id:1126352,name:Charlie Levy,character:Littlest Sister,order:13,cast_id:44,profile_path:null},{id:187983,name:Gregory Sims,character:Bill Panner,order:14,cast_id:45,profile_path:null}],crew:[{id:58726,name:Leslie Bohem,department:Writing,job:Screenplay,profile_path:null},{id:53176,name:John Gatins,department:Writing,job:Screenplay,profile_path:/A2MqnSKVzOuBf8MVfNyve2h2LxJ.jpg},{id:17825,name:Shawn Levy
答案 0 :(得分:2)
您正在搜索第一次出现的“name:”,直到“部门:指挥”,其中包含所有内容。
相反,您需要限制两个字符串之间的内容。例如,我在说组成名称的字符只能是字母数字或空格:
string.match(fixstrdirector,"name:([%w ]+),department:Directing")
或者,假设有一个逗号分隔参数,更好的方法是搜索“name:”后跟除逗号之外的任何字符,然后是“department:Directing”:
string.match(fixstrdirector,"name:([^,]+),department:Directing")
当然,如果这个名字有一个逗号,那就行不通了!
答案 1 :(得分:2)
如上所述,Lua模式为任务提供-
修饰符。正如PiL - Section 20.2所述:
+
修饰符匹配原始类的一个或多个字符。 它将始终获得与模式匹配的最长序列。与
*
类似,修饰符-
也匹配零次或多次出现 原始类的字符。但是,而不是匹配 最长的序列,它匹配最短的序列。
接下来,当您使用.
进行匹配时,它会找到满足该模式的任何和所有字符。因此,您将获得name
首次出现的结果,直到找到,department:Directing
。由于您知道它是JSON数据,因此您可以尝试匹配[^,]
;也就是说,非逗号字符。
因此,对于您的情况,请尝试:
local tAllNames = {}
for sName in fixstrdirector:gmatch( "name:([^,]-),department:Directing" ) do
tAllNames[ #tAllNames + 1 ] = sName
end
,您所需的所有名称将存储在表格tAllNames
中。可以在codepad看到上述示例。