Regular Expression for Number Less Then 60 - regex

I'm looking for a regular expression for any number from 1-60, here's what I have so far:
[0-6][0-9]
Super simple. When I input a number like 950, it catches the 50, ignores a 9 is in front.
Thanks in advance.

\b([1-9]|[1-5]\d|60)\b
You have to encapsulate it in word breaks (\b) to ensure parts of a number aren't matched (like 950).
The idea is to either match a single digit number - 1 to 9 inclusive ([1-9]), or (|) a two digit number, where the first digit is between 1 and 5 inclusive ([1-5]\d), or 60 itself.

Related

1-2 digit number, but not 0 (i.e. any number 1-99)

I'm new to regular expressions.
I'm stuck how to modify [0..9]{1,2} (this matches any 1-2 digit number I think) to match any number 1 to 99
so, how could I match any number 1-99 ?
For 1 to 99, you can use [1-9][0-9]?,

Can a Regex be written to find numbers in a string greater than x?

Can a Regex be written to find numbers in a string that are greater than x?
Say for example x = 1800
Can we find for that a number > 1800 which is in the strings: "3,000.00/month" | "$2800" | "150,000.00a month" | "only $1900" etc? The regex would find 3,000.00, 2800, 150,000.00 and 1900 because they are all greater than 1899.
What is this pattern that elludes me..
You can find any well-formatted number greater than a number with a regular expression, but it does get more complicated as the numbers get bigger.
Let's start with a simple example where x = 11, and you wanted to find any number in a string greater than 11. The regex you'd write would follow a pattern similar to the solution below:
(\d{3,}|[2-9]\d|1[2-9])(\.\d+)?|11\.\d*[1-9]\d*
REY
What is the purpose of first three inner alternations? The first captures any three or more digit number, the second any two digit number where the digit in the 10's place is 2-9, and the last any two digit number where the digit in the 10's place is 1 and the digit in the 1's place is 2-9. Then it optionally captures any decimal decimal digits.
The second outer alternation actually matches x in the integer part, but then checks the decimal part to ensure it is somehow greater. If you had a decimal part of 12 for x then you'd just do 120*\d*|1[3-9]\d*|2\d*.
There are two situations that will make the above pattern not work:
When a number contains commas
When a number starts with zeroes.
The example below deals with those cases as well and solves for x = 1800.
#FIND any number > 1800
(?=[1-9]) # ensure number doesn't begin with a zero
(
(\d{1,3},(?=\d{3}))*\d{2,3},\d{3}| # any number that has >= 5 digits with commas
\d{5,}| # any number that has >= 5 digits without commas
[2-9],?\d{3,}| # any 4 digit number that starts with 2-9
1,?9\d{2}| # any 4 digit number that starts with 1 then 2-9
1,?8[1-9]\d| # any 4 digit number that starts with 18 then 1-9
1,?80[1-9] # any 4 digit number that starts with 180 then 1-9
)
(\.\d+)? # any decimal digits
|1,?800\.\d*[1-9]\d* # any number whoses integer = 1800,
# ... then has a decimal with a non-zero digit.
REY
At the top of the regex is uses a look ahead to ensure a number actually starts with 1-9. Without it a number like 00005 would be confused as a five digit number.
The inner group finds all integer matches greater than 1800, just like the first example tried to match numbers greater than eleven. Probably, the only non obvious alternation is the first - (\d{1,3},(?=\d{3}))*\d{2,3},\d{3}. To find a 5+ digit number it matches 1-3 digits followed by a comma, then 2-3 digits, a comma and then the last 3 digits. Without the look ahead it would incorrectly match a non-number such as 234,23,412.
If you're going to be picky about commas, like I am being, then you're not going to be able to integrate comma and non-comma cases such as with 1,?9\d{2} => 1914 & 1,914. Having multiple ,? would lead to incorrect matches such as 1000,050, when trying to find a number greater than a million.
You could solve it in two steps: first find a number and then apply a regex to it that checks if it is greater than you number x.
As an example:
here is a regex that matches numbers from 31 to 99999, which you can adjust to your needs:
^(?:[3][1-9]|[4-9][0-9]|(^[1-9][0-9]{2,4}$))$
where
[3][1-9] - matches numbers from 31 to 39
[4-9][0-9] - matches numbers from 40 to 99
^[1-9][0-9]{2,4} - matches numbers from 100 to 9999
The last bit ^[1-9][0-9]{2,4} can be changed to ^[1-9][0-9]{2,} to match any number greater than 100

Regular Expression which would allow only positive integer number from 20 and above

Trying for a Regex that would allow positive integers from 20 and above upto 1000 or 10000. 0 to 19 should be allowed and also no decimal, negative and alphabets should be allowed.
I'm trying with this but not getting it.
r"[+-]?(?<!\.)\b[0-9]+\b(?!\.[0-9])"
[Update] Don't want to include number from 0 to 19.
Try something like this:
[2-9][0-9]|[1-9][0-9]{2,}
This will match a two-digit number greater than or equal to 20, or a number with three or more digits. Depending on exactly how you're using it, you may need to place start (^) and end ($) anchors around the pattern to prohibit any extra characters in the input:
^([2-9][0-9]|[1-9][0-9]{2,})$
If you want to limit this to numbers up to 1000:
^([2-9][0-9]|[1-9][0-9]{2}|1000)$
If you want to limit this to numbers up to 10000:
^([2-9][0-9]|[1-9][0-9]{2,3}|10000)$

regular expression on double(10,2)

I have this regularexpression
[0-9]+(,[0-9][0-9]?)?
it matches on 345563,24 but how can I limit the left side part on 8 characters?
88888888,00 - true because 8 characters
999999999,00 - false because 9 characters
Use this:
[0-9]{0,8}(,[0-9][0-9]?)?
{m,n} indicates the minimum and maximum number of occurrences of the previous character/group. You can indicate just a minimum or just a maximum by leaving one side of the expression blank. So the expression above would allow 0 to 8 occurrences of a digit. If you want 1 to 8 occurrences of a digit at the beginning of your expression, use this:
[0-9]{1,8}(,[0-9][0-9]?)?
I would do :
^[1-9][0-9]{,7}(,[0-9][0-9]?)?$
number (10,2) would be matched
empty string won't be matched
first digit should not be 0 (zero)
Borrowing elements from the answers of both #Kent and #user1775603, I'd try the following:
^([1-9][0-9]{0,7}|0)(,[0-9][0-9])?$
This will match:
any up-to-eight-digits number starting with a nonzero, with zero or two decimal digits after the comma
any number >= 0 and < 1 with zero or two decimals after the comma (matching 0,xx where x are digits)
Do note that the decimal separator, unless you take care to do otherwise, is very often locale-dependent. Exactly how to counteract this depends on what language and/or framework you are working with, but it's something to watch out for.
I would recommend \b instead of ^ and $ as in:
\b([1-9][0-9]{0,7}|0)(,[0-9][0-9]?)?\b
So that the input does not need to be fed alone and can be found in the middle of a large text too.

Limit length of characters in a regular expression

Is there a way to limit a regular expression to 100 characters with a regular expression?
\[size=(.*?)\](.*?)\[\/size]
So Look at me! wouldn't work.
I want to limit the numbers, only allow numbers between 1 and 100.
Is there a way to limit a regex to 100 characters WITH regex?
Your example suggests that you'd like to grab a number from inside the regex and then use this number to place a maximum length on another part that is matched later in the regex. This usually isn't possible in a single pass. Your best bet is to have two separate regular expressions:
one to match the maximum length you'd like to use
one which uses the previously extracted value to verify that its own match does not exceed the specified length
If you just want to limit the number of characters matched by an expression, most regular expressions support bounds by using braces. For instance,
\d{3}-\d{3}-\d{4}
will match (US) phone numbers: exactly three digits, then a hyphen, then exactly three digits, then another hyphen, then exactly four digits.
Likewise, you can set upper or lower limits:
\d{5,10}
means "at least 5, but not more than 10 digits".
Update: The OP clarified that he's trying to limit the value, not the length. My new answer is don't use regular expressions for that. Extract the value, then compare it against the maximum you extracted from the size parameter. It's much less error-prone.
If you want to restrict valid input to integer values between 1 and 100, this will do it:
^([1-9]|[1-9][0-9]|100)$
Explanation:
^ = start of input
() = multiple options to match
First argument [1-9] - matches any entries between 1 and 9
| = OR argument separator
Second Argument [1-9][0-9] - matches entries between 10 and 99
Last Argument 100 - Self explanatory - matches entries of 100
This will not accept:
Zero - 0
Any integer preceded with a zero - 01, 021, 001
Any integer greater than 100
If you want numbers from 1 up to 100:
100|[1-9]\d?
Limit the length of characters in a regular expression:
^[a-z]{6,15}$'
Limit length of characters or numbers in a regular expression:
^[a-z | 0-9]{6,15}$'
(^(\d{2})|^(\d{4})|^(\d{5}))$
This expression takes the number of length 2,4 and 5. Valid Inputs are
12
1234
12345
You could do a negative lookahead for the number of characters you want. So if you have a complex regex to get a specific format and you wanted to limit it to say, 50 characters. Then you could preface it with:
(?!.{51})