Solved

Regular Expression Help

Posted on 2016-09-01
9
29 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
  • 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 12

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
 
LVL 12

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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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 12

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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,…
Do you hate spam? I do, and I am willing to bet you do as well. I often wonder, though, "if people hate spam so much, why do they still post their email addresses on the web?" I'm not talking about a plain-text posting here. I am referring to the fa…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now