Karoffe Posted July 28, 2015 Share Posted July 28, 2015 Well, I want to string.gsub a whole number insead of a character in a number Example: local anyString = "This whole number: 123456 , and this just a character in a number: 5" so is there a way to string.gsub 123456 instead of 1 alone and 2 alone, etc.... Link to comment
Dealman Posted July 29, 2015 Share Posted July 29, 2015 You can do this by using regular expressions(Google it). I'm on my phone at the moment so I can't give you and examples. But there should be plenty of related threads if you try and search. What I can say is that %d stands for digit. So if there is a number, it will be deleted. You can then mix this with + to get all the numbers. string.gsub(stringVariable, "%d+", "") Link to comment
Karoffe Posted July 29, 2015 Author Share Posted July 29, 2015 Well, I knew that but wanted to use it with string.match so I extract the part I want and add \n before the number What if I don't want to change the value in string.gsub I want the number to stay as it is but adding an \n before the number, how can that be done? Link to comment
Buffalo Posted July 29, 2015 Share Posted July 29, 2015 That needs a bit more expertise. Tho this piece should work local str = "Lucky number 2569!" local e1,e2 = string.find(str,"%d+") str = string.sub(str,0,e1-1)..'/n'..string.sub(str,e1,string.len(str)) > "Lucky number /n2569!" Link to comment
MTA Team botder Posted July 29, 2015 MTA Team Share Posted July 29, 2015 local anything = "This whole number: 123456" local number = tonumber(anything:match("%d+")) print(number) Output: 123456 Link to comment
Karoffe Posted July 29, 2015 Author Share Posted July 29, 2015 That needs a bit more expertise.Tho this piece should work local str = "Lucky number 2569!" local e1,e2 = string.find(str,"%d+") str = string.sub(str,0,e1-1)..'/n'..string.sub(str,e1,string.len(str)) > "Lucky number /n2569!" Yes that would work for just 1 whole number but it won't work if there is more.. so if the str was "Lucky numbers 2569, 3662, 5 !" the output is going to be like this > "Lucky numbers /n2569, 3662, 5 !" I want the output to be like this > "Lucky numbers /n2569, /n3662, /n5 !" ... That's why I wanted to use string.match so I use (repeat-until) with inserting every extracted part to a table until there is no more numbers in the string and then draw every row in the table in the right position... local anything = "This whole number: 123456" local number = tonumber(anything:match("%d+")) print(number) Output: 123456 Yes that would work if the string was like that only, but like I stated above it won't work with numerous "whole" numbers... Link to comment
Buffalo Posted July 29, 2015 Share Posted July 29, 2015 Try string.gsub('Lucky Lottery numbers: 12, 53, 55!','%d+','/n%1') > Lucky Lottery numbers: /n12, /n53, /n55! Link to comment
Karoffe Posted July 29, 2015 Author Share Posted July 29, 2015 That works !!, Thanks. I am suprised I never knew/saw that %1 before in lua.. Thanks again, you just saved me Link to comment
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now