Thread Tools Display Modes
07-21-16, 12:49 AM   #1
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
String Patterns

Hi all,

Just got a quick question about String Pattern Match in Lua.

I was trying to switch status bar texture of Legion's new nameplate.

In order to achieve this, I'll have to get a reference to health bar frame of nameplate.

It looks like new nameplates have a name pattern of "NamePlateXUnitFrame" where X is a number.

I have gone through the following tutorials, but still unsure of how String pattern in Lua works and how I could utilize match function.

https://www.lua.org/pil/20.2.html
https://www.lua.org/pil/20.1.html
http://lua-users.org/wiki/PatternsTutorial

Could I please get a help regarding this?

Thank you.
  Reply With Quote
07-21-16, 03:32 AM   #2
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
https://wow.gamepedia.com/Pattern_matching this page might help explain it better.

The pattern would just be string:match("^NamePlate%d+UnitFrame$")
  Reply With Quote
07-21-16, 04:20 AM   #3
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by semlar View Post
https://wow.gamepedia.com/Pattern_matching this page might help explain it better.

The pattern would just be string:match("^NamePlate%d+UnitFrame$")
Hi semlar,

Thank you for your reply and that page will definitely save my life!

God... looks much neater than official Lua page...
  Reply With Quote
07-21-16, 01:56 PM   #4
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
I used a hook in the default nameplate system to access newly created nameplates in order to modify them.

Lua Code:
  1. hooksecurefunc(NamePlateDriverFrame,"OnNamePlateCreated",function(self,base)
  2. --  HealthBar is base.UnitFrame.healthBar
  3. --  Note this runs every time a UnitFrame is to be fetched or generated for a nameplate
  4. end);
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote
07-21-16, 10:10 PM   #5
Layback_
An Onyxian Warder
Join Date: Feb 2016
Posts: 358
Originally Posted by SDPhantom View Post
I used a hook in the default nameplate system to access newly created nameplates in order to modify them.

Lua Code:
  1. hooksecurefunc(NamePlateDriverFrame,"OnNamePlateCreated",function(self,base)
  2. --  HealthBar is base.UnitFrame.healthBar
  3. --  Note this runs every time a UnitFrame is to be fetched or generated for a nameplate
  4. end);
Hi SDPhantom,

that approach seems better!

I'll have a go with it

Thank you!
  Reply With Quote
07-27-16, 02:34 AM   #6
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
I have a variant question on this same theme. What if you are looking for magic characters in a string?

eg tring to find the instance of "[somestuffhere]" I have tried
  • \[
  • %[
  • [
  • \\[
  • .*?(\\[)
  • /\[/g
  • /.*?(\[)/g

Code:
local leftstr = string.find("/cast [combat]!Consecration", "%[")
I am <maxwell smart voice>this close</maxwell smart voice> from being able to translate macros from enUS to local.
__________________
BattleNet: TimothyLuke#1860
WowLazyMacros/Curse/GitHub/WowInterface: TimothyLuke

Most Commonly Played Characters:
Huldrych@Dath'Remar
Draik@Nagrand
  Reply With Quote
07-27-16, 02:59 AM   #7
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
I found a way but its not pretty

Code:
  local leftfound = false
  for i = 1, #str do
    local c = str:sub(i,i)
    if c == "[" and not leftfound then
      leftfound = true
      leftstr = i
    end
    if c == "]" then
      rightstr = i
    end
  end
__________________
BattleNet: TimothyLuke#1860
WowLazyMacros/Curse/GitHub/WowInterface: TimothyLuke

Most Commonly Played Characters:
Huldrych@Dath'Remar
Draik@Nagrand
  Reply With Quote
07-27-16, 04:37 AM   #8
Lombra
A Molten Giant
 
Lombra's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2006
Posts: 554
You escape with %.
Code:
local leftstr = string.find("/cast [combat]!Consecration", "%[")
This works for me.
__________________
Grab your sword and fight the Horde!
  Reply With Quote
07-27-16, 02:34 PM   #9
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
%b matches enclosures with the start and end char following it. For example %b[] matches anything in square brackets (don't need to escape as it's defined as part of the character class). This also handles nesting if you need it supported.

PS: The ! ability prefix no longer works in 7.0. Not that would work anyway on abilities that don't toggle.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 07-27-16 at 02:36 PM.
  Reply With Quote
07-28-16, 01:26 AM   #10
TimothyLuke
A Fallenroot Satyr
 
TimothyLuke's Avatar
AddOn Author - Click to view addons
Join Date: Jan 2010
Posts: 24
Originally Posted by SDPhantom View Post
%b matches enclosures with the start and end char following it. For example %b[] matches anything in square brackets (don't need to escape as it's defined as part of the character class). This also handles nesting if you need it supported.

PS: The ! ability prefix no longer works in 7.0. Not that would work anyway on abilities that don't toggle.
I started with %b[] and %[ but it didn't work on the Spanish (esMX) client for some reason.
__________________
BattleNet: TimothyLuke#1860
WowLazyMacros/Curse/GitHub/WowInterface: TimothyLuke

Most Commonly Played Characters:
Huldrych@Dath'Remar
Draik@Nagrand
  Reply With Quote
07-28-16, 02:45 AM   #11
semlar
A Pyroguard Emberseer
 
semlar's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2007
Posts: 1,060
Originally Posted by SDPhantom View Post
PS: The ! ability prefix no longer works in 7.0
This isn't true. Obviously it does nothing for consecration, but something like /cast !bear form will not shift you out of bear form (I just tried it).
  Reply With Quote
07-28-16, 10:56 AM   #12
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,326
I guess it was a bug they fixed. It wasn't working for a while in beta.
I'll have to redo my bars again.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » String Patterns


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