Solved

Regular Expression Help

Posted on 2016-09-01
9
31 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Exchange 2010 Transport Rule Regex 28 93
regex needed 18 75
how to extract number from string using regular expressions in oracle 3 50
PowerShell Regular Expression 2 38
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…
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…
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…

948 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

20 Experts available now in Live!

Get 1:1 Help Now