Find word in another string

Hi all,

Trying to find all words that can be made from a original word.

For instance, the word is puppet. I go through a list of words in a database, and try to find words that can be created with the letetrs in the word puppet.

Words possible:

  • pup

  • pet

  • put

I tried this code below, but that gives to many combinations, cause it uses 1 letter multiple times.

[lua]

local words = {‘puttee’,‘tutee’,‘tepee’,‘tutu’,‘putt’,‘pet’,‘put’,‘pup’}

local keyword = ‘puppet’

– convert keyword to a pattern and match it against each word

local pattern = string.format(’^[%s]+$’, keyword)

for i,word in ipairs(words) do

    local matches = word:match(pattern)

    print(word, matches and ‘matches’ or ‘does not match’)

end

[/lua]

Output:

puttee matches

tutee matches

tepee matches

tutu matches

putt matches

pet matches

put matches

pup matches

Anyone?

In your format “[%s]” resolves to the set of characters “[puet]” (dropping multiple “p”) and with the “+” any words made up of any combination of those letters will match, thus your output.

I do not have a solution, but that is the cause.

In your format “[%s]” resolves to the set of characters “[puet]” (dropping multiple “p”) and with the “+” any words made up of any combination of those letters will match, thus your output.

I do not have a solution, but that is the cause.