Old 09-13-2019, 06:21 AM   #1
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default Basic string:match with 'or' operator

I have a function that Prints the Rank and then prints the Name

What I'm expecting is
1. Name1
2. Name2
Name3EXP1
...
Name21EXP2
3. Name85
... etc...

But if there is either [EXP1] or [EXP2] in the Name, I don't want the rank printed.
Somebody can tell me what's the syntax to do this? I've tried this but it doesn't work
Code:
 if not tostring(Name):match('[EXP1]' or '[EXP2]') then
   print(Rank)
 end
   print(Name)
Thanks
lexaproductions is offline   Reply With Quote
Old 09-13-2019, 06:37 AM   #2
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

The most literal version would be:
Code:
local name = tostring(Name)
if name:match("EXP1") or name:match("EXP2") then
  ...
Note that the []s you had will mess it up, as they define a set of characters to match rather than a phrase - [EXP1] would match E, X, P, or 1. If you specifically want to match the phrase with []s you need to use %[ and %] to tell it that you mean the actual character.

However, if your ranks are all in the same format we can simplify things:
Code:
if tostring(Name):match("EXP%d+") then
%d means "any digit" and the + says "match one or more of this".
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 09-13-2019, 06:51 AM   #3
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default

The thing is that, it seems that only the first condition is verified so only EXP1 is validated.
lexaproductions is offline   Reply With Quote
Old 09-13-2019, 06:53 AM   #4
lexaproductions
Human being with feelings
 
Join Date: Jan 2013
Posts: 1,126
Default

And how would I go about putting my excluded words in a table:

Code:
WordToBeExcluded = {'Lion', 'Cat', 'Dog'}
lexaproductions is offline   Reply With Quote
Old 09-13-2019, 08:34 AM   #5
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 4,937
Default

Code:
function containsAnyOf(str, excludedWords)
  for _, word in ipairs(excludedWords) do
    if str:find(word, 1, true) then
      return true
    end
  end

  return false
end


WordsToBeExcluded = {'Lion', 'Cat', 'Dog'}
print(containsAnyOf('Hello World', WordsToBeExcluded)) --> false
print(containsAnyOf('March Comes in Like a Lion', WordsToBeExcluded)) --> true
cfillion is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:54 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.