Solved

# Regular Expression To Find Number > 5,000

Posted on 2011-03-18
565 Views
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
Question by:CCSNV
• 7
• 3

LVL 75

Expert Comment

ID: 35168828

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

LVL 75

Accepted Solution

käµfm³d   👽 earned 500 total points
ID: 35168837
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
``````
0

LVL 75

Expert Comment

ID: 35168863
P.S.

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

Author Comment

ID: 35168908
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
``````

Can you help explain this better for me?

thanks
0

LVL 75

Expert Comment

ID: 35168965
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

Author Comment

ID: 35169067
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+)?
``````

correct?
0

LVL 75

Expert Comment

ID: 35169422
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
``````
0

LVL 75

Expert Comment

ID: 35169424
My apologies for the incorrectness of my first post.
0

Author Comment

ID: 35169458
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 75

Expert Comment

ID: 35169467
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

Question has a verified solution.

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

### Suggested Solutions

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…