How detect every punctuation exept dot using string patterns?

I’m trying make TextBox, which will allow player type only Numbers and Dot (.)
How separate not-Numbers I know (%a), but how separate punctuation (%p), if I need make exeption for only 1 of them? Can someone who knows them help me, and explain me, how I can make this?

Sounds like you need this. This will match numbers and dot.
[%d\.]+
this will match anything that is not a number or a dot
[^%d\.]+

1 Like

I have 1 additional question - is it possible to allow type only 1 dot per time? like only 1.942, but not 1.94.2?

This pattern will match any set of numbers with or without a decimal.
^%d*\.?%d*$

Thank you again, I have last (I believe last) question - can I somehow “group” 2 patterns, like [^%d* (\.,)? %d*$], so it would detect \. or , 1 time?

Lua probably isn’t the right language for this if I’m being honest. These patterns are normally called “regular expressions” and they are far more powerful outside of Roblox. I think we might be suffering a little from the XY problem here. You’re telling me what you want, but not why you need it. If you let me know why you need it, I might be able to do this in an easier way. I’ll take a stab at it anyways.

You’ve said this is for player input. Do they need to input commas? You can get a number without the commas by doing this.

local number = inputString:gsub("[^\.%d]+", ""):match("^%d*\.?%d*$")

However, a single dot becomes a valid input. You’ll need to check further with tonumber to see if it is valid. Or you can start with tonumber and not worry about this.
If this isn’t what you’re looking for, tell me what you want to do. Tell me what you want to have happen when it’s a valid number, when it’s not a valid number, and what is/isn’t valid.

I’m trying make building system, and player should set manually move and rotation snap.
For me, the main problem is that I don’t understand how I can group string patterns, for example I can’t understand this:


but also, about ^ character stated this:

also, idk, why [^%something] should be used for exeption, if I can type [%SOMETHING] (something = a, l, d…)

Also, if you can, please say me - correctly I understand this:
1 - if any string patterns are covered with [ ] - they will act like 1 pattern ([%d%p] will match both digit and punctuation once)
2 - if ^ is inside [ ] - it acts similar like BIG LETTERED patern ([^%d] = %D)
3 - [ ] can’t be placed inside another [ ]
4 - ( ) used just to group patterns ((%d)(%d)(%d) applied on 718 will give 7, 1 and 8)?

1 is correct if I’m understanding you.
%d and [1234567890] do the same thing. You can also write [0-9] It just matches anything that is contained in the [].
2 is also correct.
%D and [^1234567890] do the same thing.
3 is also correct.

With 4, if you do string.match, it will return anything inside of parentheses separately.

local a, b, c = string.match("XYZ", "(%w)(%w)(%w)")
print(b) --> Y

Now you asked about Anchors. Those are represented by ^ at the beginning and $ at the end. It prevent matching in the middle of the string. For example, %S+$ will match the last word.

print(string.match("this is a test string.", "%S+$")) --> string.
print(string.match("this is a test string.", "^%S+")) --> this

If you use both, the entire input needs to match the pattern. So if we use ^%S+$, the entire string needs to match %S from beginning to end or it won’t match anything.

print(string.match("this is a test string.", "^%S+$")) --> 
print(string.match("thisisateststring.", "^%S+$")) --> thisisateststring.
1 Like