What is the difference between string.find and string.match in Lua?

I am trying to understand what is the difference between string.find and string.match in Lua. It seems to me that both find the pattern in a string. But what is the difference? And how can I use them each? Say, if I had the string "Disk Space: 3000 kB" and I wanted to extract "3000" from it.

EDIT: Well, I think I did too much and now I'm lost. Basically, I need to translate this from Perl to 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);

So far I have written this:

for Line in io.lines("/proc/meminfo") do
    if Line:find("MemTotal") then
        Mem = Line
        Mem = string.gsub(Mem, ".*", ".*", 1)
    end
end

But this is obviously wrong. What will I not get? I understand why this is wrong, and what it actually does, and why, when I do

print(Mem)

he returns

.*

but I don’t understand how to do it right. Regular expressions confuse me!

+3
1

string.match:

local space = tonumber(("Disk Space 3000 kB"):match("Disk Space ([%.,%d]+) kB"))

string.find , . , string.match , , string.find . string.find , Lua, "plain".

string.match, , string.find, , , .

+3

Source: https://habr.com/ru/post/1754774/


All Articles