How can I get all the numbers from a string with the slash included?

hello, im trying to make it so it’ll get all the numbers with the slashes being included. Is this possible?

This is my current code:

local code = "d=345/344"

local nums= string.gsub(code, "%D", "")

print(nums) --> 345344  I want it to be 345/344 

any help is appreciated thanks!

1 Like
string.split("d=345/344","d=")[2]
1 Like

To match any numbers or slashes, you can use the string pattern [%d/]+. [] matches any of the specified patterns within. In this case, it’s any number (%d), or the slash (/). (+) matches the maximum number of the preceding string pattern.

1 Like

that doesn’t do what I want.

If i change the code variable i’d have to change that.

im not sure if im doing it right, but when I do it, it’ll print “d=”.

my code:

local code = "d=345/344"

local nums= string.gsub(code, "[%d/]+", "")

print(nums) --> is d=

can you give me a code sample plz?

You’re substituting every number and slash with “”. If you want to get the pattern, use string.gmatch:

local str = "foo100/300bar200/300"
local match = string.gmatch(str, "([%d/]+)")

for capture in match do
    print(capture) --> "100/300", "200/300"
end

() just returns what’s being captured within it.

1 Like

yoo it worked ty!!!

local match = string.gmatch(str, "[%d/]+")

Just so you’re aware it isn’t necessary to ‘capture’ the result as in your case the capture is the same as the pattern you’re matching.

1 Like