Solved

Regular Expression Help

Posted on 2016-09-01
9
32 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
regular expression for 10 digit number 7 58
REReplaceNoCase help 1 33
REGEX HELP 11 43
How to parse the string and split the sub directory names in asp.net 7 41
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…
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…

777 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