|
| 23 Jan 2016 08:55 PM |
lets say we have a string... "b87b9s08b"
how would you remove all letters from a string but keep the numbers, and having the numbers in the correct order
it should wind up like this
"87908"
|
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jan 2016 08:58 PM |
You can do:
string.gsub(str, "%a+", "") |
|
|
| Report Abuse |
|
|
| |
|
|
| 23 Jan 2016 08:59 PM |
local gatherNumbers = function(str) local out = "" for v in str:gmatch("[^%D]+")do out = out .. v end return out end |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:00 PM |
local str = "dnd93md93f"
str = str:gsub("%a","") -- %a is an uppercase or lowercase letter
#code R+ | local RAP = "426,386"; local robux = "R$9,003" |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:00 PM |
| It's called a pattern. It means 1 or more alphabetical characters. 'w' is alphanumeric, 'b' is between and followed by two characters (like between parentheses '%b()') and more. Just search for string patterns on the wiki. |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:01 PM |
my bad, I forgot the + sign. Add it after the %a+.
#code R+ | local RAP = "426,386"; local robux = "R$9,003" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jan 2016 09:01 PM |
%a is the pattern for all letters (a-zA-Z) You can also do
gsub(str, "[^%d]+", "") too to remove any none digits |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:01 PM |
| Thx but what does [^%D]+ mean |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
| |
|
|
| 23 Jan 2016 09:02 PM |
| http://wiki.roblox.com/?title=Patterns |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:02 PM |
Do you want to take out all the letters or do you want to keep all the numbers?
Take out all the letters changes ABC?.,123 to ?.,123
Keep all the numbers changes ABC?.,123 to 123
string.gsub(input, "%D+", '') |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:02 PM |
| "[^%D]+" Look up string patterns. This one does not work. Cnt's works, but I would do %D+ because that (IMO) is less messy and work-around-ish than those carrot brackets. |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:02 PM |
^ anchors to the beginning of the string, %D is all NON-digits.
#code R+ | local RAP = "426,386"; local robux = "R$9,003" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jan 2016 09:03 PM |
| ^ doesn't anchor to the beginning of a string if it's in a class. |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:08 PM |
class?
#code R+ | local RAP = "427,315"; local robux = "R$9,003" |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:09 PM |
| Character class, also known as character set. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jan 2016 09:10 PM |
[%w_] is a custom class that matches either a alphanumerical character or an underscore The "^" inverts the class, so [^%w] is non-alpha numerical characters (%W accomplishes this as well) |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:11 PM |
Ohhhh... that's the complicated way for just capitalizing it to get the opposite effect...
#code R+ | local RAP = "427,315"; local robux = "R$9,003" |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jan 2016 09:13 PM |
Well I mean you sometimes have to do it. Like in the case of inverting multiple things or something that's not a "default" class.
[^abc] will match anything NOT a, b, or c |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:16 PM |
| Why don't regular expressions support left-recursion? For that matter, why doesn't Lua support regular expressions?! I dislike string patterns, they are weak. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 23 Jan 2016 09:19 PM |
| Lua's string pattern 'library' is small, regex is gigantic. That's probably the biggest reason why regex is not in Lua |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:19 PM |
| Why do I ask questions that I expect answers to right before going to bed? Good night, have fun eating jell-o and hyper chuckwallas. |
|
|
| Report Abuse |
|
|
|
| 23 Jan 2016 09:20 PM |
| Ah, that makes sense. Good night again, and have fun eating fluorescent armadillos. |
|
|
| Report Abuse |
|
|