Nginx 如何使用lua从有效uuid开始的主字符串中提取子字符串
我有一个主字符串如下 “/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.31752806054624577114682163897525068657/” 从主字符串中,我需要从uuid部分提取一个子字符串Nginx 如何使用lua从有效uuid开始的主字符串中提取子字符串,nginx,lua,openresty,Nginx,Lua,Openresty,我有一个主字符串如下 “/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.31752806054624577114682163897525068657/” 从主字符串中,我需要从uuid部分提取一个子字符串 "/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/" 我试过了 str
"/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
我试过了
string.match(“/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.31752806054647477114682163897525068657/”,“/[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[
a-fA F0-9]{4}-[a-fA F0-9]{4}-[a-fA F0-9]/
但是,如果你想获得
“/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/输出/9999.31752806054624577114682163897525068657/”
从
或者让我们假设7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0
,output
和9999.31752806054624577114682163897525068657
,这是您的模式尝试所建议的。否则,在以下解决方案中省略括号
您可以使用如下模式:
local text = "/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(text:match("/([%x%-]+)/([^/]+)/([^/]+)"))
“/([^/]+)/”
在两个斜杠之间捕获至少一个非斜杠字符
关于您的尝试:
不能在字符串模式中给出类似于{4}
的计数
你必须用%
转义-
,因为它是一个神奇的角色
()
只捕获一个字符
请阅读以了解您的错误以及如何正确使用字符串模式。也请尝试代码
s="/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(s:match("/.-/.-(/.+)$"))
它使用非贪婪匹配跳过前两个“字段”
s="/tmp/xjtscpdownload/7eb17cc6-b3c9-4ebd-945b-c0e0656a33f0/output/9999.317528060546245771146821638997525068657/"
print(s:match("/.-/.-(/.+)$"))