Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matching numbers with or without a decimal point

Tags:

regex

I've been trying for a while to find a regular expression that I need. 3 characters before . and two characters after the decimal point.

I try this

[+ -]?[0-9]{0,3}[.]?[0-9]{0,2}    # but accepted as 55555 or the 
[+ -]?[0-9]{0,3}[.][0-9]{0,2}     # but this is not accepted as the 44 

Can someone help me?

like image 936
Bobert Avatar asked Nov 28 '12 12:11

Bobert


People also ask

What if there is no decimal point?

Accuracy and Scientific Notation The decimal point is a period written between the digits of a number. If there is no decimal point, it is understood to be after the last digit on the right and there is no place (zero place) accuracy. The significant digits of a number are those digits that are most accurate.

Is a number without any decimal part?

Integers. Integers whole numbers that can be positive, negative or zero, but have no decimal places or fractional parts.

Why do we use decimal points in numbers?

It is a point or dot we use to separate the whole number part from the fractional part of a decimal number. A decimal number is a number that consists of a whole number and a fractional part. The decimal point separates the whole number from the fractional part.

Do you say point or and for decimals?

A: This is a common misconception, but in spoken or written numbers the conjunction “and” does not mean decimal point. So someone who says, “Twelve times eleven is one hundred and thirty-two” means the result is 132, not 100.32.


2 Answers

Use this:

^[+ -]?[0-9]{1,3}([.][0-9]{1,2})?$ 

See it live

I added anchors at the beginning and end. If these are ommitted 55555 produces two matches: 555 and 55.

like image 54
Daniel Hilgarth Avatar answered Sep 24 '22 01:09

Daniel Hilgarth


55555 was matched in your first attempt because you made only the decimal point optional and 44 wasn't matched in your second because you only made the decimal point not optional. What you want to do is make the decimal place and the following digits all optional.

You also need to anchor the match otherwise 123, 45.12 and 345 will match in 12345.12345 for example.

If you want to validate the string use this: ^[-+]?[0-9]{1,3}(\.[0-9]{1,2})?$

Explanation:

^                # Match the start of string
[-+]?            # Optional plus or minus
[0-9]{1,3}       # Followed by 1 - 3 digits 
(\.[0-9]{1,2})?  # Optionally followed by decimal place (escaped \) & 1-2 digits 
$                # Match the end of the string

Try it out here!

Further notes:

This will only match strings that fit the pattern i.e. 123.34

If you want to match the pattern inside a string i.e. I am 123.34 cm tall

Than use (^|\s) and (\s|$) as anchors instead:

(^|\s)[-+]?[0-9]{1,3}(\.[0-9]{1,2})?(\s|$)

Which matches the ^ start of line | any whitespace \s, the same with the end of the line $. The match will include the spaces so remember to trim(match) to removed them if needed.

like image 20
Chris Seymour Avatar answered Sep 27 '22 01:09

Chris Seymour