Quantcast
gsub and pattern failure - WoWInterface
Thread Tools Display Modes
10-01-17, 10:27 PM   #1
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,123
gsub and pattern failure

I am trying to replace text on the reputation bar. The current text looks like the following, but of course the number and status change with each watched faction update.
Code:
Valajar: 7.5K [Exalted]
What I would like to do is replace the [Exalted] portion with Paragon if the faction is paragon-level.

This is what I have so far.
Lua Code:
  1. if isParagon then
  2.     local replacement = ""
  3.     local barText = bar.text:GetText()
  4.     local gender = UnitSex("player")
  5.     local exalted = GetText("FACTION_STANDING_LABEL" .. standingID, gender)
  6.     print("My standing is ", exalted)
  7.     if E.db.PCB.reputationBar.textFormat == "P" then
  8.         replacement = L["P"]
  9.     else
  10.         replacement = L["Paragon"]
  11.     end
  12.     local match = strmatch(barText, "^%[(.+)%]")
  13.     print("My match is ", match)
  14.     --[[
  15.     barText = string.gsub(barText, "^%[" .. ("%w+"), replacement)
  16.     barText = string.gsub(barText, "%[", "")
  17.     barText = string.gsub(barText, "%]", "")
  18.     bar.text:SetText(barText)
  19.     ]]--
  20. end
Sure enough, the first test print does say "My standing is Exalted" only if the watched rep is Paragon, but the second test print outputs "My match is nil" so it isn't matching anything. If I could get it to work, the end result would look like the below for Paragon factions. For every other faction it should still say whatever text is on the rep bar by default. This also means that if a faction is Paragon-capable yet the user has not gained Paragon status it should still say [Revered] or whatever.
Code:
Valajar: 7.5K Paragon
  Reply With Quote
10-01-17, 10:33 PM   #2
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,123
About the commented section, I have tried so many variants of pattern matching I can't even keep them straight anymore. That is just the last version before I added the test prints.

Also, the original author left off the gender portion of FACTION_STANDING_LABEL .. standingID and I don't know if that matters. I put it back in for my code.
  Reply With Quote
10-02-17, 03:55 AM   #3
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 453
Remove the ^ anchor from the match pattern, you are only trying to match at the beginning of the string.
  Reply With Quote
10-02-17, 03:19 PM   #4
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,123
Whoa. Okay, next question: if I wanted to keep the [] brackets and only wanted to replace the text between them, how would I match that?

Removing the ^ worked great, so thank you for that!
  Reply With Quote
10-02-17, 03:25 PM   #5
Fizzlemizz
I did that?
 
Fizzlemizz's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Dec 2011
Posts: 1,276
replacement = "["..replacement .."]"

But I'm lazy
__________________
Fizzlemizz
Maintainer of Discord Unit Frames and Discord Art.
Author of FauxMazzle, FauxMazzleHUD and Move Pad Plus.
  Reply With Quote
10-02-17, 04:02 PM   #6
Kanegasi
A Firelord
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 499
The gender part is only for some non-English clients, since the reputation words are different based on gender. You can drop it if your client is English.

For example:

Code:
English GlobalStrings.lua:
	FACTION_STANDING_LABEL8 = "Exalted";
	FACTION_STANDING_LABEL8_FEMALE = "Exalted";

Spanish (ES) GlobalStrings.lua:
	FACTION_STANDING_LABEL8 = "Exaltado";
	FACTION_STANDING_LABEL8_FEMALE = "Exaltada";
As for your replacement, here's a simplified version that doesn't bother with brackets or anything else, drycoded:

Lua Code:
  1. if isParagon then
  2.     local replacement
  3.     if E.db.PCB.reputationBar.textFormat == "P" then
  4.         replacement = L["P"]
  5.     else
  6.         replacement = L["Paragon"]
  7.     end
  8.     bar.text:SetText(gsub(bar.text:GetText(),FACTION_STANDING_LABEL8,replacement))
  9. end

Last edited by Kanegasi : 10-02-17 at 04:18 PM. Reason: forgot to localize the replacement variable
  Reply With Quote
10-02-17, 04:37 PM   #7
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,123
Excellent!! Thank you guys
  Reply With Quote
10-03-17, 09:27 AM   #8
Rainrider
A Firelord
AddOn Author - Click to view addons
Join Date: Nov 2008
Posts: 453
If there is a faction with Exalted as part of its name, you might get undesired results:
Code:
Exalted Coders: 7.5K [Exalted]
will turn into
Code:
Paragon Coders: 7.5K [Paragon]
  Reply With Quote
10-03-17, 10:53 AM   #9
myrroddin
A Pyroguard Emberseer
 
myrroddin's Avatar
AddOn Author - Click to view addons
Join Date: Oct 2008
Posts: 1,123
Originally Posted by Rainrider View Post
If there is a faction with Exalted as part of its name, you might get undesired results:
Code:
Exalted Coders: 7.5K [Exalted]
will turn into
Code:
Paragon Coders: 7.5K [Paragon]
I realized that too, although it is a rare case, I should think. However, I decided to stick to a suggestion above and start the gsub at the "[" symbol which ought to prevent that outlier case.
Code:
local barText = bar.text:GetText()
barText = gsub(barText, "%[(.+)%]", replacement)
Unless someone finds it not to work when the addon goes live, I'm going with this as the solution.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » gsub and pattern failure

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

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