Lua中string.find和string.match有何本质区别?
- 内容介绍
- 文章标签
- 相关推荐
本文共计656个文字,预计阅读时间需要3分钟。
我尝试了理解Lua中的`string.find`和`string.match`之间的区别。对我来说,似乎都在字符串中找到了一个模式。但它们有什么区别呢?我应该怎么使用它们呢?比如,如果我有字符串Disk Space: 3000 kB,我想从中提取3000。
`string.find`用于查找子字符串的位置,并返回第一个匹配的位置索引。如果找不到匹配项,则返回`nil`。
`string.match`用于匹配模式并返回匹配的结果。如果没有匹配,则返回`nil`。
使用示例:
lualocal str=Disk Space: 3000 kBlocal pattern=:%d+ kB
local pos, match=string.find(str, pattern)if pos then print(Found: .. match)else print(No match found)end
local result=string.match(str, pattern)if result then print(Match: .. result)else print(No match found)end
输出:
Found: :3000 kBMatch: :3000 kB
在这个例子中,`string.find`返回匹配模式的位置索引,而`string.match`返回整个匹配的字符串。
我试图了解Lua中string.find和string.match之间的区别.对我而言,似乎都在字符串中找到了一个模式.但有什么区别?我该如何使用?比方说,如果我有字符串“Disk Space:3000 kB”,我想从中提取’3000′.编辑:好的,我觉得我过于复杂,现在我迷路了.基本上,我需要翻译这个,从Perl到Lua:
my $mem; my $memfree; open(FILE, 'proc/meminfo'); while (<FILE>) { if (m/MemTotal/) { $mem = $_; $mem =~ s/.*:(.*)/$1/; } elseif (m/MemFree/) { $memfree = $_; $memfree =~ s/.*:(.*)/$1/; } } close(FILE);
到目前为止我写的这个:
for Line in io.lines("/proc/meminfo") do if Line:find("MemTotal") then Mem = Line Mem = string.gsub(Mem, ".*", ".*", 1) end end
但这显然是错误的.我得不到什么?我理解为什么它是错的,它实际上在做什么以及为什么我这样做
print(Mem)
它返回
.*
但我不明白什么是正确的方法.正则表达式让我困惑!
在您的情况下,您需要string.match:local space = tonumber(("Disk Space 3000 kB"):match("Disk Space ([%.,%d]+) kB"))
string.find略有不同,因为在返回任何捕获之前,它返回找到的子字符串的开始和结束索引.当没有捕获时,string.match将返回匹配的整个字符串,而string.find将不会返回超过第二个返回值的任何内容. string.find还允许您使用’plain’参数搜索字符串,而不会知道Lua模式.
当你想要匹配的捕获时使用string.match,当你想要子串的位置时,或者当你想要位置和捕获时,使用string.find.
本文共计656个文字,预计阅读时间需要3分钟。
我尝试了理解Lua中的`string.find`和`string.match`之间的区别。对我来说,似乎都在字符串中找到了一个模式。但它们有什么区别呢?我应该怎么使用它们呢?比如,如果我有字符串Disk Space: 3000 kB,我想从中提取3000。
`string.find`用于查找子字符串的位置,并返回第一个匹配的位置索引。如果找不到匹配项,则返回`nil`。
`string.match`用于匹配模式并返回匹配的结果。如果没有匹配,则返回`nil`。
使用示例:
lualocal str=Disk Space: 3000 kBlocal pattern=:%d+ kB
local pos, match=string.find(str, pattern)if pos then print(Found: .. match)else print(No match found)end
local result=string.match(str, pattern)if result then print(Match: .. result)else print(No match found)end
输出:
Found: :3000 kBMatch: :3000 kB
在这个例子中,`string.find`返回匹配模式的位置索引,而`string.match`返回整个匹配的字符串。
我试图了解Lua中string.find和string.match之间的区别.对我而言,似乎都在字符串中找到了一个模式.但有什么区别?我该如何使用?比方说,如果我有字符串“Disk Space:3000 kB”,我想从中提取’3000′.编辑:好的,我觉得我过于复杂,现在我迷路了.基本上,我需要翻译这个,从Perl到Lua:
my $mem; my $memfree; open(FILE, 'proc/meminfo'); while (<FILE>) { if (m/MemTotal/) { $mem = $_; $mem =~ s/.*:(.*)/$1/; } elseif (m/MemFree/) { $memfree = $_; $memfree =~ s/.*:(.*)/$1/; } } close(FILE);
到目前为止我写的这个:
for Line in io.lines("/proc/meminfo") do if Line:find("MemTotal") then Mem = Line Mem = string.gsub(Mem, ".*", ".*", 1) end end
但这显然是错误的.我得不到什么?我理解为什么它是错的,它实际上在做什么以及为什么我这样做
print(Mem)
它返回
.*
但我不明白什么是正确的方法.正则表达式让我困惑!
在您的情况下,您需要string.match:local space = tonumber(("Disk Space 3000 kB"):match("Disk Space ([%.,%d]+) kB"))
string.find略有不同,因为在返回任何捕获之前,它返回找到的子字符串的开始和结束索引.当没有捕获时,string.match将返回匹配的整个字符串,而string.find将不会返回超过第二个返回值的任何内容. string.find还允许您使用’plain’参数搜索字符串,而不会知道Lua模式.
当你想要匹配的捕获时使用string.match,当你想要子串的位置时,或者当你想要位置和捕获时,使用string.find.

