lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi

On 10 April 2012 13:14, SAN THO <saminside34@gmail.com> wrote:
>   i was writing a lua script for searching a pattern in a multiline string.
> the code snippet looks like below :
>
>    local text="multi-line huge string chunk"
>    local pattern = "hello world"
>    print("String found " .. string.match(text,pattern))
>
> and this give me a result like below :
>
>    String found b
>
> my intention was to make a condition based action if string found, but here
> i was getting "b".

string.match() returns nil if the pattern was not found or the pattern
if it was, because in Lua, "false" or "nil" are false values and
anything else is considered true.
So this will work:

if string.match(text,pattern) then
   something()
else
   other_thing()
end

To convert the result to a real "true" or "false", you can use

print("String found " .. not not string.match(text,pattern))

Happy learning with Lua

    M