Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by ClearRoseOfWar · Feb 02, 2016 at 06:23 AM · programmingregex

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? :)

Comment
Add comment · Show 7
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ClearRoseOfWar · Feb 02, 2016 at 04:39 PM 0
Share

Any Regexperts out there that can help RegNoob? :D

avatar image ClearRoseOfWar · Feb 02, 2016 at 08:47 PM 0
Share

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?

avatar image ClearRoseOfWar · Feb 02, 2016 at 09:09 PM 0
Share

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 :)

avatar image brunocoimbra ClearRoseOfWar · Feb 03, 2016 at 01:18 AM 0
Share

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-.]"`
avatar image ClearRoseOfWar · Feb 05, 2016 at 04:30 PM 0
Share

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?

avatar image Firedan1176 ClearRoseOfWar · Feb 05, 2016 at 05:01 PM 0
Share

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.

avatar image Taxen0 · Feb 05, 2016 at 06:46 PM 0
Share

"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!

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

42 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

PROBLEM IN MOVING WITH C # 1 Answer

Question about Input.GetKey 1 Answer

Modular entity construction 0 Answers

What is wrong with this code? 2 Answers

Confused about a script that is changing a gameobjects material colour but not sure what the issue is. 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges