Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Match several (mutually exclusive) alternatives with string.find()?
Started by Divergent Monkey Jan 02 2019 12:10 PM

- - - - -
7 replies to this topic
regexp string find
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 350 posts
  • Corona SDK

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?



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,602 posts
  • Corona SDK

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.



[TOPIC: post.html]
#3

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

i dont see how that would be possible no



[TOPIC: post.html]
#4

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 350 posts
  • Corona SDK

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.



[TOPIC: post.html]
#5

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,602 posts
  • Corona SDK

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.



[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,602 posts
  • Corona SDK

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



[TOPIC: post.html]
#7

anaqim

[GLOBAL: userInfoPane.html]
anaqim
  • Contributor

  • 770 posts
  • Corona SDK

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.



[TOPIC: post.html]
#8

Divergent Monkey

[GLOBAL: userInfoPane.html]
Divergent Monkey
  • Contributor

  • 350 posts
  • Corona SDK

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




[topic_controls]
[/topic_controls]