I am currently making a number plate system which is generated from a player’s UserId and I came across these questions :
- How do I remove the 0 and replace it with a blank text so that it will become WXM547?
- What if it generates XXX0002? How do I remove the 000 in the middle of XXX and 2?
- For example it generates XXX0020, How do I make it so that it would not delete the last number but just the first two zeros?
I have researched and seen people using string.find or string.gmatch, but I have no clue how to execute it.
I look forward to your assistance c:
2 Likes
You can use a pretty simple string pattern for this:
local license = license:gsub("0+([^0]+0*)$", "%1")
Basically, this matches as many zeroes as possible in a row, any number of non-zero characters, then any number of zeroes before the end of the string. It replaces all that with everything after the non-zero characters.
If you need to remove multiple isolated sets of zeroes (e.g. 10203), you’d have to loop this until there’s no zeroes. Here’s a sloppy example of how that could work:
local pattern = "0+([^0]+0*)$"
while license:match(pattern) do
license = license:gsub(pattern, "%1")
end
There’s probably a better way to do this, but this is what I came up with off the top of my head.
Edit: Here’s another way simpler solution:
license = license:gsub("0+([^0])", "%1")
Removes any zeroes that have something besides a zero after them.
5 Likes
Hi posatta. I have tried it and it has answered my question, but I am confused on what is: ("0+([^0])", "%1")
Thank you so much for your help!
Those are two string patterns. The 0+
represents any number of zeroes (+
is for 1 or more). The parentheses around the [^0]
match it as a capture, and [^0]
matches any non-zero (^ negates the pattern) character. In the second pattern, %1
just represents the replacement string. %1
, %2
, %3
, etc. all represent a capture, which is what the parentheses from earlier were for. So, basically, it takes any number of zeroes and then a non-zero character, replacing it with just that non-zero character.
3 Likes
Scripting is confusing, but your explaination has made it clearer.
1 Like