如何使用 lua 从有效的 uuid 开始从主字符串中提取子字符串
How to extract a substring from main string starting from valid uuid using lua
我有一个主字符串如下
"/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
我需要从主字符串中提取一个从 uuid 部分开始的子字符串
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
我试过了
string.match("/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/", "/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[
a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/(.)/(.)/$
但是运气不好。
如果你想获得
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
来自
"/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
或者说 7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0
、output
和 9999.317528060546245771146821638997525068657
,因为这是您的模式尝试所建议的。否则在下面的解决方案中省略括号。
您可以使用这样的模式:
local text = "/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(text:match("/([%x%-]+)/([^/]+)/([^/]+)"))
"/([^/]+)/"
捕获两个斜线之间至少一个非斜线字符。
您的尝试:
您不能在字符串模式中给出像 {4}
这样的计数。
你必须用 %
转义 -
因为它是一个魔法字符。
(.)
只会捕获一个字符。
请阅读 Lua manual 以找出您做错了什么以及如何正确使用字符串模式。
也试试代码
s="/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(s:match("/.-/.-(/.+)$"))
它使用非贪婪匹配跳过前两个 "fields"。
我有一个主字符串如下 "/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
我需要从主字符串中提取一个从 uuid 部分开始的子字符串
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
我试过了
string.match("/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/", "/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[
a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}/(.)/(.)/$
但是运气不好。
如果你想获得
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
来自
"/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
或者说 7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0
、output
和 9999.317528060546245771146821638997525068657
,因为这是您的模式尝试所建议的。否则在下面的解决方案中省略括号。
您可以使用这样的模式:
local text = "/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(text:match("/([%x%-]+)/([^/]+)/([^/]+)"))
"/([^/]+)/"
捕获两个斜线之间至少一个非斜线字符。
您的尝试:
您不能在字符串模式中给出像 {4}
这样的计数。
你必须用 %
转义 -
因为它是一个魔法字符。
(.)
只会捕获一个字符。
请阅读 Lua manual 以找出您做错了什么以及如何正确使用字符串模式。
也试试代码
s="/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(s:match("/.-/.-(/.+)$"))
它使用非贪婪匹配跳过前两个 "fields"。