Thread Tools Display Modes
04/19/14, 01:56 AM   #1
gumphrey
Join Date: Apr 2014
Posts: 5
Problems with string.gsub

I am trying to get plain text out of chat messages. I want to strip all the color codes, link information, etc. just leaving a plain text version of the message. Is there an existing method exposed that can do that?

I have been experimenting with using string.gsub to remove the text, but it doesn't work except for very simple patterns. For instance, in stand alone lua I can use this pattern to find item links and replace them with just plain text in square brackets. In my addon, it does nothing...

local textOut = string.gsub(textIn, 'H.-h(%[.-%])h', '%1')
  Reply With Quote
04/19/14, 02:09 AM   #2
gumphrey
Join Date: Apr 2014
Posts: 5
After some more experimentation, I think the problem is the % codes. Is something different used? I tried "normal" regex syntax, and that doesn't seem to work either...
  Reply With Quote
04/19/14, 02:09 PM   #3
gumphrey
Join Date: Apr 2014
Posts: 5
Nevermind, % codes work sometimes. It just seems busted when you do certain things (different than stock Lua which makes no sense to me), and it is rather unpredictable.
  Reply With Quote
04/19/14, 08:22 PM   #4
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 641
Lua Code:
  1. function ZO_LinkHandler_ParseLink(link)
  2.     if type(link) == "string" then
  3.         local color, data, text = link:match("|H(.-):(.-)|h(.-)|h")
  4.         return text, color, zo_strsplit(':', data)
  5.     end
  6. end
That is for a totally different thing, but I was wondering if it might help. It might strip out everything. See if it works, if not I am not good at Lua yet so I can't offer advice. That code was to me from someone else.
  Reply With Quote
04/19/14, 09:05 PM   #5
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by gumphrey View Post
I am trying to get plain text out of chat messages. I want to strip all the color codes, link information, etc. just leaving a plain text version of the message. Is there an existing method exposed that can do that?

I have been experimenting with using string.gsub to remove the text, but it doesn't work except for very simple patterns. For instance, in stand alone lua I can use this pattern to find item links and replace them with just plain text in square brackets. In my addon, it does nothing...

local textOut = string.gsub(textIn, 'H.-h(%[.-%])h', '%1')
Item link starts with "|H" then it contains clolor code, link type etc and ends with "|h" then there is item name (or just text to disply) and "|h" again. If I'm not mistaken, there are no [] in item name, so you should change pattern.
lua Code:
  1. local textOut = string.gsub(textIn, "|H.-|h(.-)|h", "%1")
I'm not in game, so I did not try it.
  Reply With Quote
04/20/14, 07:38 AM   #6
ins
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 76
There are two types of itemlinks.
With or without [].

* GetItemLink(*integer* _bagId_, *integer* _slotIndex_, *[LinkStyle|#LinkStyle]* _linkStyle_)
  Reply With Quote
04/20/14, 09:58 AM   #7
Sharlikran
 
Sharlikran's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 641
Originally Posted by ins View Post
There are two types of itemlinks.
With or without [].

* GetItemLink(*integer* _bagId_, *integer* _slotIndex_, *[LinkStyle|#LinkStyle]* _linkStyle_)
YES! I have been wondering about that. I will have to revisit this. I have been really bothered that sometimes when I parse the link I get "corn flower" and other times I get "corn flower^p" I bet it's that link style.
  Reply With Quote
04/20/14, 12:17 PM   #8
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
Originally Posted by Sharlikran View Post
YES! I have been wondering about that. I will have to revisit this. I have been really bothered that sometimes when I parse the link I get "corn flower" and other times I get "corn flower^p" I bet it's that link style.
These meta-characters are part of nearly all strings, even crafting station names have them and they are not an item nor can they be linked
  Reply With Quote
04/20/14, 12:46 PM   #9
gumphrey
Join Date: Apr 2014
Posts: 5
Thanks everyone, that clears up a lot. The part I missed was the vertical bar, it was tricky to debug since putting any string out in any way to display ends up interpreting the vertical bars. Indeed everything works now and Lua is not behaving inconsistently as I first thought. Is there a raw dump which doesn't interpret all the strings (I may write a helper function for that now that I know what is going on)

Also out of curiosity what are all the other numbers in the link?

The links I see seem to be of the form:
|H<color>:<type?>:<id?>:<n1>:..:<n19>|h<linkname>|h

The color is clear. What are the possibilites for type (I am assuming it is the type of thing getting linked... I usually see "item" there). Is the next number the ID for that type (item ID for instance)? Anyone know what all the other numbers are (I generally see 19 more, but maybe that is dependent on what is getting linked?)

Last edited by gumphrey : 04/21/14 at 07:07 PM.
  Reply With Quote
04/20/14, 01:18 PM   #10
ins
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 76
See my reply:
http://www.esoui.com/forums/showpost...3&postcount=13

or:
http://wiki.esoui.com/ZO_LinkHandler_CreateLink

For information on item links.
  Reply With Quote
04/20/14, 02:17 PM   #11
gumphrey
Join Date: Apr 2014
Posts: 5
Perfect, thanks!
  Reply With Quote
04/20/14, 03:03 PM   #12
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
Originally Posted by Iyanga View Post
These meta-characters are part of nearly all strings, even crafting station names have them and they are not an item nor can they be linked
You can get rid of that characters using zo_strformat function, see this info:
http://www.esoui.com/forums/showthread.php?p=5007
  Reply With Quote
04/20/14, 03:50 PM   #13
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
AddOn Author - Click to view addons
Join Date: Feb 2014
Posts: 648
To clarify on why those characters are there, it's so that all the text around that item name (or npc name) can match.

^m, ^f, ^n = masculine, feminine, neutral (so surrounding text can switch to his, her, etc. or German can use the appropriate der, das, die, etc.)
^p = plural (so surrounding text can switch between it, them, etc.)

Are there others I'm forgetting?
  Reply With Quote
04/21/14, 12:00 PM   #14
Iyanga
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 183
Originally Posted by Seerah View Post
To clarify on why those characters are there, it's so that all the text around that item name (or npc name) can match.

^m, ^f, ^n = masculine, feminine, neutral (so surrounding text can switch to his, her, etc. or German can use the appropriate der, das, die, etc.)
^p = plural (so surrounding text can switch between it, them, etc.)

Are there others I'm forgetting?

There are multi-letter meta-characters like ^ms. So it might be masculine singular, but then it's strange that not all meta-characters use two characters.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Problems with string.gsub

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