Thread Tools Display Modes
06/28/23, 12:44 AM   #1
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
Question string.find or string.match question

Hey ive been having some irregularities with one of my teleport functions. Particularly with my blacklist.

I have "Tel Galen" in my blacklist and have trouble teleporting to "Galen". I guess what I'm needing is a way to exactly match not match part of "Tel Galen".

Heres a sort of example of what I'm using to achieve what I have now.

Code:
local _, _, memberZone, _, _, _, _, memberzoneID = GetGuildMemberCharacterInfo(guildId, memberIndex)

if not MyAddon.ZoneBlacklist(memberZone) then
     teleport to blah blah blah
end

function MyAddon.ZoneBlacklist(value)
     local zones = {
     "Tel Galen",
     }
     for _, zoneName in ipairs(zones) do
          if string.match(zoneName, value) then return true end
     end
     return false
end
Whats happening is when someone is in "Galen" and it does the blacklist check its returning true for "Tel Galen" and not performing the teleport. I need some way to ensure "Galen" zone returns false like an exact match start to finish not partial. Any suggestions? Im wondering if just reversing zoneName, value in the string.match to value, zoneName might do the job and look for "Tel Galen" in "Galen".

Last edited by sinnereso : 06/28/23 at 12:49 AM.
  Reply With Quote
06/28/23, 12:59 AM   #2
ExoY
 
ExoY's Avatar
AddOn Author - Click to view addons
Join Date: Feb 2020
Posts: 88
Thumbs up

Wouldnt just compare strings directly solve the problem?

Lua Code:
  1. if string1 == string2 then
  2.  -- do stuff if they are the same
  3. end

or

Lua Code:
  1. if string1 ~= string2 then
  2.  -- do stuff if they are not the same
  3. end

depending on implementation
  Reply With Quote
06/28/23, 01:09 AM   #3
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
Originally Posted by ExoY View Post
Wouldnt just compare strings directly solve the problem?

Lua Code:
  1. if string1 == string2 then
  2.  -- do stuff if they are the same
  3. end

or

Lua Code:
  1. if string1 ~= string2 then
  2.  -- do stuff if they are not the same
  3. end

depending on implementation
LOL! omg I was soo into working with the string.find etc I missed the obvious thank you
  Reply With Quote
06/28/23, 02:37 AM   #4
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
Again: Use zoneIds instead of strings and you never get that problems!!!
Using the IDs internaly does not remove the possibilities to show the names as strings to your UI?!

One day you might improve on it then ;-)

Last edited by Baertram : 06/28/23 at 03:46 AM.
  Reply With Quote
06/28/23, 08:57 AM   #5
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
Originally Posted by Baertram View Post
Again: Use zoneIds instead of strings and you never get that problems!!!
Using the IDs internaly does not remove the possibilities to show the names as strings to your UI?!

One day you might improve on it then ;-)
I've been struggling with the ID's as the different queries for groups, friends and guilds had wildly inconsistant zone, subzone and internal ID's being returned. Or in some situations I couldn't pre-detect it. It may have been related to my imperfect string matching I was previously doing though. I'll revisit this soon.

Last edited by sinnereso : 06/28/23 at 08:59 AM.
  Reply With Quote
06/28/23, 04:57 PM   #6
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
If you want the unique difference then MapId is better.
  Reply With Quote
06/29/23, 04:19 PM   #7
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
so far its working perfectly and havent been able to break what I have running..

I do have some questions regarding:

Code:
SLASH_COMMANDS["/rd"] = function (option)--<< /RD TELEPORT TO ZONE OR SPECIFIC PLAYER HOUSE
	if option == "" then df(RidinDirty.logo .. "  /rd partialzonename => overland zones") df(RidinDirty.logo .. "  /rd exact@name partialhousename => player houses") return end
	local options = {}
    local searchResult = { string.match(option,"^(%S*)%s*(.-)$") }--<<<<< THESE SYMBOLS
	for i,v in pairs(searchResult) do
        if (v ~= nil and v ~= " ") then
            --options[i] = string.lower(v)
			options[i] = v
		end
    end
	if (options[3] ~= nil and options[3] ~= "") then df(RidinDirty.logo .. "  /rd partialzonename => overland zones") df(RidinDirty.logo .. "  /rd exact@name partialhousename => player houses") return end
	if (options[2] ~= nil and options[2] ~= "") then
		RidinDirty.Teleport(options[1], options[2])
	else
		RidinDirty.Teleport(options[1])
	end
end
im weak on those symbols i got from a sample someplace for string finding and matching.. What is there seems to function OK for 1 or 2 words but if theres a 3rd its gets wonky...

-What im trying to do with them is if theres no "options" then display a help text..
-if theres options[1] and options[2] then if it contains an "@" do my code for teleport to specific player house which its currently doing fine if there 2 words but not checking for "@"
-if theres options[1] options[2] options[3]++++ then display help text

I google like mad to find the meaning of thes symbols but theres some wild explanations what make it hard to make sense of.

id like it todo as follows if a user or myself type these:

/rd => display df("blah blah") help text
/rd word => find player in zone"word" and go there
/rd word word => goto specific player house if @symbol in word1 and find a house matching word2 or if no @symbol in word1 then => display df("blah blah") help text
/rd word word word or more words => display df("blah blah") help text

Last edited by sinnereso : 06/29/23 at 04:31 PM.
  Reply With Quote
06/29/23, 05:12 PM   #8
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
this seems to do what I need.. any thoughts?

Code:
SLASH_COMMANDS["/rd"] = function (option)--<< /RD TELEPORT TO ZONE OR SPECIFIC PLAYER HOUSE
	if option == "" then df(RidinDirty.logo .. "  /rd partialzonename => overland zones") df(RidinDirty.logo .. "  /rd exact@name partialhousename => player houses") return end
	local options = {}
    local searchResult = { string.match(option, "^(%S*)%s*(%S*)%s*(%S*)%s*(%S*)$") }
	for i,v in pairs(searchResult) do
        if (v ~= nil and v ~= "") then
            --options[i] = string.lower(v)
			options[i] = v
		end
    end
	if (options[3] ~= nil and options[3] ~= "") or (options[4] ~= nil and options[4] ~= "") then
		df(RidinDirty.logo .. "  /rd partialzonename => overland zones") df(RidinDirty.logo .. "  /rd exact@name partialhousename => player houses")
	elseif (options[2] ~= nil and options[2] ~= "") then
		if string.find(options[1], "^(@)") then
			RidinDirty.Teleport(options[1], options[2])
		else
			df(RidinDirty.logo .. "  /rd partialzonename => overland zones") df(RidinDirty.logo .. "  /rd exact@name partialhousename => player houses")
		end
	else
		RidinDirty.Teleport(options[1])
	end
end

Last edited by sinnereso : 06/29/23 at 05:20 PM.
  Reply With Quote
06/29/23, 06:27 PM   #9
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
^(%S*)%s*(%S*)%s*(%S*)%s*(%S*)$

My thought, WOW!

https://www.lua.org/pil/20.2.html

So you are telling it to check at the beginning of the string with the ^ symbol. Group 1 you look for Zero or more non space chars. Then for the default group (which isn't really a group) you look for Zero or more spaces. Then Group 2 you look for Zero or more non space chars. The default group again for Zero or more spaces. Then Group 3 you look for Zero or more non space chars. Then a third time, the default group again for Zero or more spaces. Then Group 4 you look for Zero or more non space chars. The last group matches up to the end of the string with the $ symbol, as long as it isn't a space.

The default group might actually be group 1 for Lua which means that you have 1 through 5 rather then the default and 1 through 4.

Last edited by Sharlikran : 06/29/23 at 08:27 PM.
  Reply With Quote
06/29/23, 06:50 PM   #10
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
Code:
  local searchByWords = zo_strgmatch(searchText, '%S+')
  for theWord in searchByWords do
    <<do stuff>>
  end
You can use zo_strgmatch() or string.gmatch() as an iterator.

Last edited by Sharlikran : 06/29/23 at 06:57 PM.
  Reply With Quote
06/29/23, 07:38 PM   #11
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
ive looked through all of those like 100x.. and amazingly this is working and sort of makes sense to me.. try it youll see.

the %S is for basically the words and the %s is for the spaces... seems to be working perfectly..

What i cant find any info on is the S in %S. But based on that example it seemed it was doing the seperate words.
  Reply With Quote
06/29/23, 08:07 PM   #12
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
Quoting myself because you missed it.
So you are telling it to check at the beginning of the string with the ^ symbol. Group 1 you look for Zero or more non space chars. Then for the default group (which isn't really a group) you look for Zero or more spaces. Then Group 2 you look for Zero or more non space chars. The default group again for Zero or more spaces. Then Group 3 you look for Zero or more non space chars. Then a third time, the default group again for Zero or more spaces. Then Group 4 you look for Zero or more non space chars. The last group matches up to the end of the string with the $ symbol, as long as it isn't a space.
On the page I listed it tells you what the capital version does and I even said what it does in my previous post, 4 times. Sorry. Good luck with that.

Last edited by Sharlikran : 06/29/23 at 08:26 PM.
  Reply With Quote
06/29/23, 09:22 PM   #13
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
Originally Posted by Sharlikran View Post
Quoting myself because you missed it.


On the page I listed it tells you what the capital version does and I even said what it does in my previous post, 4 times. Sorry. Good luck with that.
Theres not a capital S on that page!!!

and all im needing is /rd option[1] option[2] option[3] option[4] etc
  Reply With Quote
06/29/23, 10:46 PM   #14
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
The following table lists all character classes:

. all characters
%a letters
%c control characters
%d digits
%l lower case letters
%p punctuation characters
%s space characters
%u upper case letters
%w alphanumeric characters
%x hexadecimal digits
%z the character with representation 0

An upper case version of any of those classes represents the complement of the class. For instance, '%A' represents all non-letter characters
You can't search for only the answer you seek, just the uppercase S. If you read it and see that the lowercase s is a space then the uppercase is all non spaces.

Code:
  local searchByWords = zo_strgmatch(searchText, '%S+')
  for theWord in searchByWords do
    <<do stuff>>
  end
Which is why that would return 3 words if searchText was "The Zone Name" similar to splitting the string using a space for the split.

Last edited by Sharlikran : 06/30/23 at 12:13 AM.
  Reply With Quote
06/29/23, 11:01 PM   #15
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
Originally Posted by Sharlikran View Post
You can't search like a squirrel on crack for the answer you seek, just the uppercase S. If you read it and see that the lowercase s is a space then the uppercase is all non spaces.

Code:
  local searchByWords = zo_strgmatch(searchText, '%S+')
  for theWord in searchByWords do
    <<do stuff>>
  end
Which is why that would return 3 words if searchText was "The Zone Name" similar to splitting the string using a space for the split.
well thats why it works then.. I was looking for ALL non-space characters. Thank you for the compliment though.

Thank you for the help.. I havent come across anything that states the capitol S was the reverse which IS highly useful.

Last edited by sinnereso : 06/29/23 at 11:17 PM.
  Reply With Quote
06/29/23, 11:24 PM   #16
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
- What is there seems to function OK for 1 or 2 words but if there a 3rd its gets wonky
- What i cant find any info on is the S in %S. But based on that example it seemed it was doing the separate words.
- "^(%S*)%s*(%S*)%s*(%S*)%s*(%S*)$"

So I explained what the S was and showed a way to handle 1 or more words. For some reason you are frustrated with the fact the link didn't have an S on it, which is concerning.

I get you are asking but it's concerning when people ask questions and then when someone answers they put it on the person providing the answer that they did or said something wrong. Usually some rhetoric follows at some point like, "I can ask because it's the help section! Geez I'm just asking." which is just an excuse for their reaction. It's like damed if I do, damed if I don't and I'm the jerk either way. When that's not the case because I answered the question, politely, the first time. They were the ones whining about it and saying they can't figure it out.

Code:
function HandleSlashCommands(allArgs)
  local argument1 = ""
  local argument2 = ""
  local argument3 = ""

  local argNum = 0
  for word in zo_strgmatch(allArgs, "%S+") do
    argNum = argNum + 1
    if argNum == 1 then argument1 = word end
    if argNum == 2 then argument2 = word end -- which could have an @ in it
    if argNum == 3 then argument3 = word end -- use tomunber() if it's an integer
  end
  argument1 = string.lower(argument1) -- to make sure you find what you want in the string
  -- lower the 2nd or 3rd argument if needed

  if argument1 == "help" then
    <<do stuff>>
  end

  local someBooleanVar = <<your code>> -- to detect if the argument has an @
  -- use argument2 ~= "" or argNum == 2, or argNum >= 2
  if argument1 == "travel" and (argument2 ~= "" and someBooleanVar) then -- like when the 2nd argument has an @ in it
    <<do stuff to travel to house>>
  end

<< and so on>>

Last edited by Sharlikran : 06/29/23 at 11:31 PM.
  Reply With Quote
06/29/23, 11:44 PM   #17
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
I didnt suggest you said anything wrong.. unless you personally feel that way for suggesting i was searching like a squirrel on crack or something for example... I do try to search for my own answers and post here as little as possible mostly for this reason. Forums are for the most part less friendly than prison. I refused to even join one for like 15yrs because I saw the kind of bantering that goes on while googling whatever it was I needed at the time... Nearly the least friendly place on the internet hands down.

If you want to help you need to be more like beartram. This guy has suffered through many of my questions and always kept his cool. You have to realize not everyone is at your level of understanding and while something you respond with might seem simple and basic, could be right out the field of view for someone else and require re-reading it many time after more research for it to click.

Last edited by sinnereso : 06/29/23 at 11:57 PM.
  Reply With Quote
06/30/23, 12:10 AM   #18
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 655
Originally Posted by sinnereso View Post
I didnt suggest you said anything wrong
Originally Posted by sinnereso View Post
Theres not a capital S on that page!!!

and all im needing is /rd option[1] option[2] option[3] option[4] etc
It's okay. I answered politely at first anyway provided a link with a relevant answer and what you needed for the slash command. Eventually provided a working function similar to what I use. It's not helpful at all like Baertram. He is always nice to me too so food for thought. Maybe some day I can be as nice as he is.

Last edited by Sharlikran : 06/30/23 at 12:19 AM.
  Reply With Quote
06/30/23, 12:11 AM   #19
Baertram
Super Moderator
 
Baertram's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 4,989
Well, I had the same discussion with you a few weeks ago, and I must sayb
Yes, you search by your own and that's great.
But no, you often do not seem to read the answers properly.
Or you do but not going into detail, letting others (us!) do your job there...


Shatlikran explained it really good, in detsil, linked the description of the lua pattern characters (yes, that uppercase S wasn't explained but uppercase was below all chars, by the A example. So maybe you missed it, or only searched for S ).

I know it's hard to lrarn new stuff but we REALLY explain alreary more than usual, even write total code for you. And Sharlikran is correct to react annoyed of one needs to repeat the same 3 times.

Forums are not unfriendly. You must keep in mind that there are thousands of ppl asking stuff, many things were asked so often you do not even understand why it's not found by the next asking guys themselves.
And if it feels to take like an endless anserr, with multiple repeats, it's not helping. You feel like taking against a wall then (especially if different answering devs explain, provide info AND links, and it is still ignored/not understood).


So please keep that in mind and try to read the links and sources we provide more sincerely. Understand keywords klike pattern in this case here) and search on that. Google would have shown you multiple results for "lua pattern %S" (even with reddit examples where devs explain all!).

Makes you learn it better/more easily, and unstresses us ppl answering here. Else you might get only 1 link next time, and have to do the research by your own (like it would be best to learn it, actually) until you find your answer and can place the next answwuestion based on it.

If you feel this is not what forums should do for you you might need to go back to the friendly prison guys then


@Shalikran Try to strip those squirrel on crack comparisons please

Editb Sorry for the many typos, hope it's still readable.
Btw, just read that "Baertram is always nice" part: I'm not.
I jusr adopt to your questions and know meanwhile how to take you, sinnereso. I really must say it's more complicated than others, but I know you got no developer background and thus it might be harder to follow at some steps. So I'm doing the extra mile, but you cannot expect that to happen from all here. So please do not compare me with others and say "you all should be like Baetram".thanks for the words but that's unfair to the others! In the end I should be more like them be let you do more homework

Last edited by Baertram : 06/30/23 at 12:18 AM.
  Reply With Quote
06/30/23, 12:16 AM   #20
sinnereso
AddOn Author - Click to view addons
Join Date: Oct 2022
Posts: 245
Sharlikran: I wasn't jabbing. I'm the one being attacked multiple times now for asking a question or not understanding the answer.. Either way is that ok? Is that what this forum is for or about? Would you prefer to be here all alone knowing it all talking amongst yourself?

Last edited by sinnereso : 06/30/23 at 12:18 AM.
  Reply With Quote

ESOUI » Developer Discussions » General Authoring Discussion » string.find or string.match question


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

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