WoWInterface

WoWInterface (https://www.wowinterface.com/forums/index.php)
-   AddOn Help/Support (https://www.wowinterface.com/forums/forumdisplay.php?f=3)
-   -   string.gsub() and regular expression (https://www.wowinterface.com/forums/showthread.php?t=58565)

Platine 01-24-21 03:44 PM

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 ?

briskman3000 01-24-21 04:15 PM

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, "%,", ""), "%.", "");


Seerah 01-24-21 05:11 PM

Lua Code:
  1. string.gsub("12,345", ",", "")
Substitute the comma with nothing (taking it out).

Kanegasi 01-24-21 05:18 PM

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.

Platine 01-25-21 09:17 AM

Yes, this is the solution.

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

Thanks, Kanegasi.

Seerah 01-25-21 03:53 PM

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.)

Vrul 01-25-21 05:09 PM

Quote:

Originally Posted by Seerah (Post 338380)
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."

Seerah 01-26-21 02:00 PM

Of course. I wasn't thinking that he wanted to pass that entire string through it.


All times are GMT -6. The time now is 04:22 AM.

vBulletin © 2021, Jelsoft Enterprises Ltd
© 2004 - 2020 MMOUI