RegEx help: Number Should only be able to contain 1 Decimal [^\d-.]*\.
Hi There!
I am a beginner in Regex. I have been working with regex for the last 12 hours, and have figured out some small tid bits.
I have this:
[^\d-.]
Which currently only allows the use of numbers (negative or positive) and the use of decimals. I would like to limit the amount decimals to 1 point.
This:
[^\d-.]*\.
Omits all decimals.
Can someone please help me? :)
Any Regexperts out there that can help RegNoob? :D
On the regexpal.com website, the following regex pattern works:
^-?\d+(,\d+)*(\.\d)
and turns -0.5.5 into -0.5
However, with unity it does nothing. Why do some patterns work with unity, and other just dont?
Its funny how I've come to learn this sub-code, and it works on the website I provided in the last comment... But it doesn't work with unity at all.
That said, this code sorta does what I want:
[+-]?(\d)+\.+(\d{0,3})
So if I have -0.555.55 it replaces it with -0.555 and omits the rest
as soon as I make another decimal point ex. (-0.555.55.555) It then keeps everything..
Help Please $$anonymous$$e!!! :D :)
Are you using the "@" character before the regex string? Like:
@"[^\d-.]"`
Cause, in C#, the backslash (\) character is a special character used, for example, to create newline (\n) or allow the use of (") in strings (\"). Using the "@" before the string will tell to C# to read the string as it is being write, skipping that special character check.
another option is to add one backslash to each backslash used, like:
"[^\\d-.]"`
I think I;ve been looking at regular expressions too long... They are starting to piss me off now.
Can anyone tell me the difference between javascript regex, and unity regex? (If any) I don't think there is..
And on regexpal, they use a forward slash at the beginning, and at the end, along with a g for global. I assume the forward slashes indicate the beginning and ending of a string?
Also, Am I supposed to place this g in my code too? or is unity already registering as global?
Regex is very confusing for beginners, so I'd also suggest you check out http://regexr.com, which will give you real time feedback. When you paste in your regex into C#, it should behave as a string. But the '\' symbol in most program$$anonymous$$g languages is an escape character, meaning it will escape the standard string to do something else. So that's where you run into problems when you paste it into your code. When you add your regex string into your code, it will think you want to escape '\d' but in reality, you want C# to treat it as a string, not a literal. Therefore, you have to put another '\' in front of anywhere you find a '\' in your regex.
Sometimes, $$anonymous$$onoDevelop will automatically detect if you're pasting in a regex string and put in the extra '\' for you.
It's a little annoying, but it has to be done.
"Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems."
In all seriousness I tend to avoid regex unless it's the last resort, is the problem you are trying to solve simply to make sure that a number gets rounded to 1 decimal point there are far easier solutions.
If you on the other hand do this with the goal to actually learn regex then it's of course another matter =)
Either way i wish you best of luck!
Your answer
![](https://koobas.hobune.stream/wayback/20220612062654im_/https://answers.unity.com/themes/thub/images/avi.jpg)