Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Regular Expression Help

Posted on 2016-09-01
Medium Priority
45 Views
I need a regular expression that will identify  decimal values that are  less 3 digits in length..

For example:

1.0  --> Yes
2.00 --> Yes
0.01 -->  Yes
0.008 --> No
4  --> no
0
Question by:tmajor99
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 3
• 2

LVL 5

Expert Comment

ID: 41780518
``````^[0-9]*(?:\.[0-9]{1,2})?\$
``````
0

LVL 13

Expert Comment

ID: 41780534
``````^\d{1,2}\.\d{1,2}\$
``````
0

LVL 5

Expert Comment

ID: 41780543
Jeff, the regex you provided wouldn't allow for 111.0 which according to the question should be a valid match. A correction to mine though:
``````^[0-9]*\.[0-9]{1,2}\$
``````
As 4 should fail but 4.0 shouldn't.
0

LVL 13

Expert Comment

ID: 41780647
The specs say "less 3 digits in length".  so 11.0 or 1.11 would match, but not 111.0 because that is 4 digits.  The question doesn't say, but I'm assuming that there won't be a number without a leading or trailing digit on the decimal.  so 0.01 for example and not just .01

I'm assuming 4 doesn't match because its not a decimal value.
0

LVL 35

Assisted Solution

Terry Woods earned 500 total points (awarded by participants)
ID: 41780734
I'm assuming the following are invalid:
111.
.111
111

This isn't the most efficient pattern to run, but performance is not usually the problem with a regex (it's usually the correctness or edge cases that are a problem):
``````^(?=\d+\.\d+)(?!(\d.*){4}).*\$
``````

(?=\d+\.\d+) is a lookahead that requires at least one digit before and after a decimal point
(?!(\d.*){4}) is a negative lookahead that invalidates any values with 4 or more digits
0

LVL 35

Expert Comment

ID: 41780738
Jeff, your pattern will match 11.11, which I understand is incorrect.
TechieMD, yours will match 111111.1 and also .1, which I understand is incorrect.
0

LVL 5

Assisted Solution

TechieMD earned 500 total points (awarded by participants)
ID: 41780799
There is insufficient data from the examples in the question to extrapolate whether the asker wants to see only three digits in total or a maximum of two decimal places.

0

LVL 13

Accepted Solution

Jeff Darling earned 1000 total points (awarded by participants)
ID: 41781490
@Terry

Thanks, I missed that test case.  It wasn't listed.

``````^\d{1,1}\.\d{1,2}\$|^\d{1,2}\.\d{1,1}\$
``````

If the correct spec is Zero leading or  trailing digits, then that would change the pattern.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

by Batuhan Cetin Regular expression is a language that we use to edit a string or retrieve sub-strings that meets specific rules from a text. A regular expression can be applied to a set of string variables. There are many RegEx engines for uâ€¦
Whatever be the reason, if you are working on web development side, Â you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registrationâ€¦
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will wâ€¦
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: Tâ€¦
###### Suggested Courses
Course of the Month9 days, 20 hours left to enroll