Search code examples
lua

How to find a word in a single long string?


I want to be able to copy and paste a large string of words from say a text document where there are spaces, returns and not commas between each and every word. Then i want to be able to take out each word individually and put them in a table for example...

input:
please i need help

output:
{1, "please"},
{2, "i"},
{3, "need"},
{4, "help"}
(i will have the table already made with the second column set to like " ")

havent tried anything yet as nothing has come to mind and all i could think of was using gsub to turn spaces into commas and find a solution from there but again i dont think that would work out so well.


Solution

  • Your delimiters are spaces ( ), commas (,) and newlines (\n, sometimes \r\n or \r, the latter very rarely). You now want to find words delimited by these delimiters. A word is a sequence of one or more non-delimiter characters. This trivially translates to a Lua pattern which can be fed into gmatch. Paired with a loop & inserting the matches in a table you get the following:

    local words = {}
    for word in input:gmatch"[^ ,\r\n]+" do
        table.insert(words, word)
    end
    

    if you know that your words are gonna be in your locale-specific character set (usually ASCII or extended ASCII), you can use Lua's %w character class for matching sequences of alphanumeric characters:

    local words = {}
    for word in input:gmatch"%w+" do
        table.insert(words, word)
    end
    

    Note: The resulting table will be in "list" form:

    {
        [1] = "first",
        [2] = "second",
        [3] = "third",
    }
    

    (for which {"first", "second", "third"} would be shorthand)

    I don't see any good reasons for the table format you have described, but it can be trivially created by inserting tables instead of strings into the list.