Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Lua:`.-`具有可选结束字符的模式匹配_Lua_Pattern Matching - Fatal编程技术网

Lua:`.-`具有可选结束字符的模式匹配

Lua:`.-`具有可选结束字符的模式匹配,lua,pattern-matching,Lua,Pattern Matching,我正在尝试匹配路径的第一部分,例如以下字符串中的“a”: /a/b/c /a/b /a/ /a 模式“^/(.-)/”在除最后一个之外的所有情况下都有效,当我插入一个?以使最后一个斜杠成为可选时,它完全停止工作,这是为什么 > = string.match("/a/", "^/(.-)/") a > = string.match("/a", "^/(.-)/") nil > = string.match("/a", "^/(.-)/?") > 它不会停止工作。它成功

我正在尝试匹配路径的第一部分,例如以下字符串中的“a”:

/a/b/c
/a/b
/a/
/a
模式
“^/(.-)/”
在除最后一个之外的所有情况下都有效,当我插入一个
以使最后一个斜杠成为可选时,它完全停止工作,这是为什么

> = string.match("/a/", "^/(.-)/")
a
> = string.match("/a", "^/(.-)/")
nil
> = string.match("/a", "^/(.-)/?")

>

它不会停止工作。它成功地找到了空字符串的正确匹配项。试试
^/([^/]*)
谢谢,这样行得通。完全没有意识到缺失的
nil
意味着什么。它不会停止工作。它成功地找到了空字符串的正确匹配项。试试
^/([^/]*)
谢谢,这样行得通。完全没有意识到缺失的
nil
意味着什么。