Quantcast
WoWInterface - View Single Post - Pattern matching
View Single Post
10-22-19, 05:44 PM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 1,961
Originally Posted by Yukyuk View Post
This will work just fine is the itemId is the biggest number in the message.
But it will fail if there is a bigger number in the message.

It there a way to extract the first number from a text string?
Lua always returns the first match in a string. The problem with this is since the color code for the string is a hex value, it can contain numeric characters, which will sometimes get captured instead depending on the item quality. You can further define what to grab by defining the pattern better.
Code:
return tonumber(line:match("|Hitem:(%d+)")) or 0;
Notes: The parenthesis define what to capture in a pattern. This defaults to the entire pattern if no captures are defined. Also, this is a shortcut way to use string.match() since the string table is set as the metatable for all strings. string.match() also returns strings exclusively if not nil. tonumber() converts this into a number and will pass nil through. or handles the next step by providing a default value if nil is passed through. Lastly, return is not a function, it's part of the language structure. It'll lead to some confusion further down the road if you write it like a function call.
__________________
ESOUI AddOns | 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