Match several (mutually exclusive) alternatives with string.find()?

Is it possible to match several (mutually exclusive) alternatives with string.find()? For instance:

string.find("I am playing a game", "play")

I would like the pattern (in this case “play”) to match “play” OR “played” OR “playing” but NOT “playeding” without calling string.find() three times, one for each. Is that possible?

Confused.  Are you saying you’d like to call a function once for a string like this:

“I play games because playing games is fun, but replaying played games is boring.”

And have it (in a single call) tell you the positions of:

  • play 3
  • playing 22
  • played …

but to ignore replaying?

You can make your own function to do this, but string.find() needs to be called repeatedly to get that kind of info.

Something like this:

function string.multifind( str, pattern ) local found = {} local i = 1 while( i != nil ) do i = str:find( pattern, i ) if( i ~= nil ) then found[#found+1] = i i = i + 1 end end if( #found \< 1) then return nil end return found end

returns nil or table of first indexes for each ‘find’

Note: This DOES NOT handle the rule for NOT matching play embedded in a word.  You’ll need to make this a bit more elaborate to do that.

i dont see how that would be possible no

Sorry for the confusion! What I meant was that I would like to construct one single pattern that matches “play” OR “played” OR “playing” but NOT “playeding”. In pseudo-code:

string.find(mystring, "play" OR "played" OR "playing")

This should match regardless of whether mystring is “I am playing a game”, “I played a game” or “I play a game”. It should however not match “i am playeding a game”. 

I was hoping that the pattern item “?” could be used. Something like play(ed)?(ing)? or whatever the syntaxt for that would be.

I don’t know how to do a complex match + exclusion pattern like that.

You could use my ‘DIY’ method/recipe and write your own find that allows exclusions though.

It is likely there is a way to do it, but probably involving a pseudo-complex regular expression

https://www.gammon.com.au/scripts/doc.php?lua=string.find

must be easier to use it as intended, something like…

string.find(mystring,"play") OR string.find(mystring,"played") etc

… instead of re-inventing the wheel.

Ok, too bad. Thanks anyway to both of you for taking the time to answer my question.

Confused.  Are you saying you’d like to call a function once for a string like this:

“I play games because playing games is fun, but replaying played games is boring.”

And have it (in a single call) tell you the positions of:

  • play 3
  • playing 22
  • played …

but to ignore replaying?

You can make your own function to do this, but string.find() needs to be called repeatedly to get that kind of info.

Something like this:

function string.multifind( str, pattern ) local found = {} local i = 1 while( i != nil ) do i = str:find( pattern, i ) if( i ~= nil ) then found[#found+1] = i i = i + 1 end end if( #found \< 1) then return nil end return found end

returns nil or table of first indexes for each ‘find’

Note: This DOES NOT handle the rule for NOT matching play embedded in a word.  You’ll need to make this a bit more elaborate to do that.

i dont see how that would be possible no

Sorry for the confusion! What I meant was that I would like to construct one single pattern that matches “play” OR “played” OR “playing” but NOT “playeding”. In pseudo-code:

string.find(mystring, "play" OR "played" OR "playing")

This should match regardless of whether mystring is “I am playing a game”, “I played a game” or “I play a game”. It should however not match “i am playeding a game”. 

I was hoping that the pattern item “?” could be used. Something like play(ed)?(ing)? or whatever the syntaxt for that would be.

I don’t know how to do a complex match + exclusion pattern like that.

You could use my ‘DIY’ method/recipe and write your own find that allows exclusions though.

It is likely there is a way to do it, but probably involving a pseudo-complex regular expression

https://www.gammon.com.au/scripts/doc.php?lua=string.find

must be easier to use it as intended, something like…

string.find(mystring,"play") OR string.find(mystring,"played") etc

… instead of re-inventing the wheel.

Ok, too bad. Thanks anyway to both of you for taking the time to answer my question.