Solved

Regular Expression Help

Posted on 2016-09-01
9
34 Views
Last Modified: 2016-09-22
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
Comment
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
  • Learn & ask questions
  • 3
  • 3
  • 2
9 Comments
 
LVL 5

Expert Comment

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

Open in new window

0
 
LVL 13

Expert Comment

by:Jeff Darling
ID: 41780534
^\d{1,2}\.\d{1,2}$

Open in new window

0
 
LVL 5

Expert Comment

by:TechieMD
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}$

Open in new window

As 4 should fail but 4.0 shouldn't.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:Jeff Darling
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

by:Terry Woods
Terry Woods earned 125 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}).*$

Open in new window


To help you adjust it if you need to:
(?=\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

by:Terry Woods
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

by:TechieMD
TechieMD earned 125 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.

Waiting for asker clarification.
0
 
LVL 13

Accepted Solution

by:
Jeff Darling earned 250 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}$

Open in new window


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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I have been reconstructing a PHP-based application that has grown into a full blown interface system over the last ten years by a developer that has now gone into business for himself building websites. I am not incredibly fond of writing PHP code o…
As most anyone who uses or has come across them can attest to, regular expressions (regex) are a complicated bit of magic. Packed so succinctly within their cryptic syntax lies a great deal of power. It's not the "take over the world" kind of power,…
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

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question