tags:

views:

1091

answers:

7

Hi,

I've spent half an hour trying to get this, maybe someone can come up with it quickly.

I need a regular expression that will match one or two digits, followed by an optional decmial point, followed by one or two digits.

For example, it should match these strings in their entirety:


3
33
.3
.33
33.3
33.33

and NOT match anything with more than 2 digits before or after the decmial point.

Thank you, Eric

+1  A: 

You mentioned that you want the regex to match each of those strings, yet you previously mention that the is 1-2 digits before the decimal?

This will match 1-2 digits followed by a possible decimal, followed by another 1-2 digits but FAIL on your example of .33

\d{1,2}\.?\d{1,2}

This will match 0-2 digits followed by a possible deciaml, followed by another 1-2 digits and match on your example of .33

\d{0,2}\.?\d{1,2}

Not sure exactly which one you're looking for.

Hawker
This is the closest I had, but it matches 333 and 3333
SP
You said the decimal point was optional.
Jeremy Stein
@Ahawker you forgot to escape the period.
Jeremy Stein
@Ahawker, you are right. By my description, your solution was correct. I was not clear.
SP
+5  A: 

EDIT: Changed to fit other feedback.

I understood you to mean that if there is no decimal point, then there shouldn't be two more digits. So this should be it:

\d{0,2}(\.\d{1,2})?

That should do the trick in most implementations. If not, you can use:

[0-9]?[0-9]?(\.[0-9][0-9]?)?

And that should work on every implementation I've seen.

Lee
This is how I would do it, except, use \d{0,2} or [0-9]{0,2} to match the given example .33
Daniel Vandersluis
Which doesn't get .33. Try changing the first to \d{0,2}.
David Thornley
You also need to anchor the expression to satisfy the "and NOT match anything with more than 2 digits before or after the decmial point." requirement.
Daniel Vandersluis
In order to anchor it properly, I need to know whether to anchor on whitespace or beginning/end of line or whatever. Information I don't have, so I'll leave that as an exercise for the student.
Lee
This worked perfectly. Thank you all for your help!
SP
Note that this one does match 333.33 (which I think you said it should only match up to two digits, but dont know if you mentioned if it should match more than two), it also matches 33.333 (three digits in the decimal area.
Brian
A: 
^\d{0,2}\.?\d{1,2}$
cpjolicoeur
+1  A: 

To build on Lee's answer, you need to anchor the expression to satisfy the requirement of not having more than 2 numbers before the decimal.

If each number is a separate string, you can use the string anchors:

^\d{0,2}(\.\d{1,2})?$

If each number is within a string, you can use the word anchors:

\b\d{0,2}(\.\d{1,2})?\b
Daniel Vandersluis
+2  A: 
(?<![\d.])(\d{1,2}|\d{0,2}\.\d{1,2})?(?![\d.])

Matches:

  • Your examples
  • 33.

Does not match:

  • 333.33
  • 33.333
Jeremy Stein
This one may be better than the "Chosen" answer.
Brian
A: 

^(\d{0,2}\.)?\d{1,2}$

"\d{1,2}$" matches a 1-2 digit number with nothing after it (3,33,etc), "(\d{0,2}.)?" matches optionally a number 0-2 digits long followed by a period (3., 44., ., etc). Put them together and you've got your regex.

A: 

A previous answer is mostly correct, but it will also match the empty string. The following would solve this.

^([0-9]?[0-9](\.[0-9][0-9]?)?)|([0-9]?[0-9]?(\.[0-9][0-9]?))$