Thread Tools Display Modes
01-24-21, 03:44 PM   #1
Platine
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Dec 2010
Posts: 72
string.gsub() and regular expression

I need to get text without a comma in numbers using string.gsub() function:

Code:
local text = "The number 45,678 is big, but 1,234 is small."
text = string.gsub(text, "[0-9],[0-9]", "???")
print (text)
-- should be: "The number 45678 is big, but 1234 is small."
What should be the third parameter in the string.gsub() function ?
  Reply With Quote
01-24-21, 04:15 PM   #2
briskman3000
A Flamescale Wyrmkin
 
briskman3000's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2009
Posts: 108
back in legion, this is how I got the numbers, and only the numbers, out of the artifact power item tooltips without seperators. You can modify this to do what you want (I would guess).

Code:
appower = string.match(_G[self:GetName().."TextLeft"..i]:GetText(), "%d+%,?%.?%s?%d*");						
appower = string.gsub(string.gsub(appower, "%,", ""), "%.", "");
__________________
My Addons: Convert Ratings Honor Track
  Reply With Quote
01-24-21, 05:11 PM   #3
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Lua Code:
  1. string.gsub("12,345", ",", "")
Substitute the comma with nothing (taking it out).
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-24-21, 05:18 PM   #4
Kanegasi
A Molten Giant
 
Kanegasi's Avatar
AddOn Author - Click to view addons
Join Date: Apr 2007
Posts: 666
Lua does not use regex. It has a similar but limited engine:

https://wow.gamepedia.com/Pattern_matching

In this case, since I assume you want to keep the comma in the center, you need %d with parenthesis and then %# in the replacement argument

Lua Code:
  1. local text = "The number 45,678 is big, but 1,234 is small."
  2. text = string.gsub(text, "(%d),(%d)", "%1%2")
  3. print(text)
  4. -- should be: "The number 45678 is big, but 1234 is small."

The %d,%d captures any comma surrounded by numbers, the parenthesis "saves" the two numbers, and %1%2 outputs those two numbers in place without the comma.

Last edited by Kanegasi : 01-24-21 at 06:18 PM.
  Reply With Quote
01-25-21, 09:17 AM   #5
Platine
A Cliff Giant
AddOn Author - Click to view addons
Join Date: Dec 2010
Posts: 72
Yes, this is the solution.

text = string.gsub(text, "(%d),(%d)", "%1%2")

Thanks, Kanegasi.
  Reply With Quote
01-25-21, 03:53 PM   #6
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Is there any benefit to doing it that way? The method briskman3000 and I posted seems about twice as fast when benchmarking. (Though the processing time probably doesn't matter much for the OP's needs.)
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote
01-25-21, 05:09 PM   #7
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
Originally Posted by Seerah View Post
Is there any benefit to doing it that way? The method briskman3000 and I posted seems about twice as fast when benchmarking. (Though the processing time probably doesn't matter much for the OP's needs.)
The OP only wanted commas between numbers removed, not all commas.

Using the OP's sample text:
Code:
"The number 45,678 is big, but 1,234 is small."
Kanegasi's result:
Code:
"The number 45678 is big, but 1234 is small."
Other result:
Code:
"The number 45678 is big but 1234 is small."
  Reply With Quote
01-26-21, 02:00 PM   #8
Seerah
Fishing Trainer
 
Seerah's Avatar
WoWInterface Super Mod
Featured
Join Date: Oct 2006
Posts: 10,860
Of course. I wasn't thinking that he wanted to pass that entire string through it.
__________________
"You'd be surprised how many people violate this simple principle every day of their lives and try to fit square pegs into round holes, ignoring the clear reality that Things Are As They Are." -Benjamin Hoff, The Tao of Pooh

  Reply With Quote

WoWInterface » AddOns, Compilations, Macros » AddOn Help/Support » string.gsub() and regular expression

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