Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:39:33+00:00 2026-05-15T17:39:33+00:00

I am trying to understand what the difference is between string.find and string.match in

  • 0

I am trying to understand what the difference is between string.find and string.match in Lua. To me it seems that both find a pattern in a string. But what is the difference? And how do I use each? Say, if I had the string “Disk Space: 3000 kB” and I wanted to extract the ‘3000’ out of it.

EDIT: Ok, I think I overcomplicated things 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’ve 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 it is obviously wrong. What am I not getting? I understand why it is wrong, and what it is actually doing and why when I do

print(Mem)

it returns

.*

but I don’t understand what is the proper way to do it. Regular expressions confuse me!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:39:34+00:00Added an answer on May 15, 2026 at 5:39 pm

    In your case, you want string.match:

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

    string.find is slightly different, in that before returning any captures, it returns the start and end index of the substring found. When no captures are present, string.match will return the entire string matched, while string.find simply won’t return anything past the second return value. string.find also lets you search the string without being aware of Lua patterns, by using the ‘plain’ parameter.

    Use string.match when you want the matched captures, and string.find when you want the substring’s position, or when you want both the position and captures.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 501k
  • Answers 501k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How about: getattr(foo.__class__, <field_name>) This should give you the field… May 16, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer See the functions save and load. May 16, 2026 at 2:18 pm
  • Editorial Team
    Editorial Team added an answer I solved this by doing the following: In index.haml (at… May 16, 2026 at 2:18 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I am trying to understand the difference between phones, and the software on them.
I am new to COM and trying to understand the difference between STA and
In trying to understand the correlation between program objects and data in tables (here:
I'm working on a Silverlight project and I'm trying to understand the differences between
I'm trying to understand the ControlStyle property in Delphi 2007, but I'm having trouble
I am a C guy and I'm trying to understand some C++ code. I
Trying to understand What is IConnectionPoint and how this is connected to IConnectionPointContainer,IEnumConnectionPoints,IEnumConnections and
I'm trying to learn how to best use OOP in PHP. Please be aware
I am trying to learn all the new goodies that come with C# 4.0.
I am trying to understand the core of object oriented programming for php or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.