3

I've seen a few questions about Regex so far, and some were very helpful, but I'm having some trouble getting the result that I need...

One of the questions that has helped me learn a lot is this one: Regular expression to match numbers with or without commas and decimals in text

The expression that was privided in the above question is :

(:?^|\s)((?:[1-9]\d{0,2}(?:(?:,\d{3})*|\d*)|0|(?=\.))(?:\.\d*[1-9])?)(?!\S)

It will allow things like:

100,000
999.999
90.0009
1,000,023.999
0.111
.111
0

...and not like:

1,1,1.111
000,001.111
999.
0.
111.110000
1.1.1.111
9.909,888

Now that's fine, but I do not want to allow:

0.111
.111
0

I don't want to allow anything more than 2 decimal places, so 100.333 wouldn't be accepted whereas 100.33 would pass just fine. I also don't want anything starting with 0 to be passed through.

This is my first time dealing with regex and I understand stackoverflow gave me a few suggestions while asking this question which I've looked at, but I really do not understand this right now -- if you could help me understand what it is that I need to change and why, I would really appreciate that.

Community
  • 1
  • 1
Matthew
  • 673
  • 1
  • 7
  • 16
  • 1
    JS and .NET regexes are *very* different, and the `/` characters are only separators for literals, they're not part of the regex per se. If your tool doesn't expect them, simply don't include them in the pattern. I removed the .NET tag to prevent people from providing answers you wouldn't be able to use in JS. – Lucas Trzesniewski May 17 '15 at 09:40

1 Answers1

3

You can use the following:

(:?^|\s)((?:[1-9]\d{0,2}(?:(?:,\d{3})*|\d*)|(?=\.))(?:\.\d{0,2})?)(?!\S)
                                           ^              ^^^^^

Change \d*[0-9] to \d{0,2}

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
  • That was very quick! Thanks for pointing out what you changed. It still allows it to start with a decimal or a 0 though, is it possible to change that? – Matthew May 17 '15 at 09:39
  • I just have to wait a couple minutes to accept your answer! Thank you very much for helping me out! – Matthew May 17 '15 at 09:44
  • Hi, I have a question for you, I'd like to make the maximum number of numbers allowed on the left of the decimal place be 5 numbers, how would I do this? If its not a problem =) – Matthew May 21 '15 at 10:57
  • @Matthew sure.. use `(:?^|\s)((?:[1-9]\d{0,2}(?:(?:,\d{3})?)|(?=\.))(?:\.\d{0,2})?)(?!\S)` – karthik manchala May 21 '15 at 11:22
  • Thanks! So I see what you've changed, just for future reference what if I wanted to make the maximum amount of numbers allowed on the left of the decimal 4? I thought I'd see the number 5 or something, but I'm still lost as to how that works! -- I'm trying to understand it though – Matthew May 21 '15 at 11:31
  • 1
    @Matthew It would have been easy to understand if your pattern was simple.. you have to understand the regex for making changes. Once you know the meaning of the above regex it will be easy for you to edit. To answer your question,.. for 4 digits it would be much more simple `(:?^|\s)((?:(?:\d,\d{3})|\d{0,4})|(?=\.))(?:\.\d{0,2})?(?!\S)` concentrate on the patterns that you want to allow for 4 digits.. `\d`, `\d\d`, `\d\d\d`, `\d\d\d\d` or `\d,\d\d\d` hence `(\d,\d{3})|\d{0-4}` – karthik manchala May 21 '15 at 12:36