Solved

Regular Expression To Find Number > 5,000

Posted on 2011-03-18
10
538 Views
Last Modified: 2012-05-11
Hi folks, I am learning expressions and am looking for a way to search for any number that is greater than 5,000.

When used it could find a number like 5093 or 5093.53 ect basically anything greater than 5,000. Can you help me out a little. Explaination is good, not just solution, trying to learn not just copy. :)

0
Comment
Question by:CCSNV
  • 7
  • 3
10 Comments
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility

5\d{3}(?:\.\d+)?

Explanation:
=============================================================
5             Literal
\d{3}         3 digits
(?: ... )?    Non-capturing group with a zero-or-one modifier (applied to entire group)
\.            Literal period
\d+           One-or-more digits

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Correction:
\d*5\d{3}(?:\.\d+)?

Explanation:
=======================================================================================
\d*           Zero-or-more digits (should capture numbers with 5 or more digits
5             Literal
\d{3}         3 digits
(?: ... )?    Non-capturing group with a zero-or-one modifier (applied to entire group)
\.            Literal period
\d+           One-or-more digits

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
P.S.

The above does NOT take into account commas (grouping separators), so 5,000 would not be found.
0
 

Author Comment

by:CCSNV
Comment Utility
So not having to worry about commas or decimals is ok for example i can search 5,001.02 by typing in 500102. I tried it and it works.

Not quite understanding the  
\d{3}         3 digits

Open in new window


Can you help explain this better for me?

thanks
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
In some regex engines, "\d" is shorthand for the character class "[0-9]", which means any character that is 0, 1, 2, 3, 4, 5, 6, 7, 8, or 9--basically, any digit. The construct "{...}" is a quantifier, much the same way that * and + are. It has 4 forms:

{n} -- exactly n occurrences
{n,m} -- between n and m occurrences, inclusive
{n,} -- between n and infinity occurrences, inclusive ({1,} is the same as +)
{,m} -- between 0 and m occurrences, inclusive (the same as *)

Before we go further, perhaps I should clarify one thing:  are we speaking in terms of a specific language, or are you talking about regular expressions as it pertains to theory? There are some differences between the two.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:CCSNV
Comment Utility
as it pertains in theory. so i have eliminated the .NN so now i am just searching for items that are 5000 and larger so 5000 is written as 500000 no comma no decimal but need the range to be anything greater than 500000(5,000).

so if I had 5000.31 in my tables it is listed as 500031.  So to do this I should be able to use:

 
\d*6\d{3}(?:\d+)?

Open in new window


correct?
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I need to correct one thing in my original post. The original post will find values that have a 5 in the fourth position from the right. It will not find what you asked for originally.

We can of course correct that. Following should find any value greater than 5000, and I've added the modification you mention in your last post. I am also providing and updated explanation.
\d{7,}|[5-9]\d{5}

Explanation:
=================================================================================================
\d{7,}       Five-or-more ( {5,} ) digits ( \d )        | This will handle numbers 10,000 and up
|            OR
[5-9]        Any single digit that is 5, 6, 7, 8, or 9  | This will handle numbers
\d{5}        Exactly Three ( {3} ) digits ( \d )        | 5,000 - 9,999

Open in new window

0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
My apologies for the incorrectness of my first post.
0
 

Author Comment

by:CCSNV
Comment Utility
no worries thank you for the assistance. I am now starting to understand. Sometimes reading "dictionary" explaination are confusing but you have broke it down down for me in what I was looking for.

Thanks again for the assistance.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
I found the following site when I was first learning regex:  www.regular-expressions.info . I think it is very well put together. There are of course numerous sites which cover regex, but that is the one I have experience with  = )
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

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…

771 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

10 Experts available now in Live!

Get 1:1 Help Now