Is there a standard way to determine only maximum length? [duplicate] - regex

This question already has answers here:
Regular expression to limit number of characters to 10
(6 answers)
Closed 3 years ago.
I have a case that I only care about the maximum number.
For instance, consider that I have to simply check that the string is: "max of 10 numeric digits", which means that it should meet the following:
It contains only numbers (resolved).
It has to be 10 digits at maximum.
I read about limiting the length, I came up with the following result:
^\d{10}$: all numerics, 10 numbers specifically.
^\d{10,20}$: all numerics, 10 - 20 length.
^\d{10,}$: all numerics, 10 at minimum.
However, ^\d{,10}$ is invalid! Is there a specific way to do it, or should I do it as ^\d{1,10}$ ?

^\d{1,10}$ or anubhava's advice in the comment might simply suffice, yet there might be other excessive expressions such as:
^(?=[0-9]).{1,10}$
^(?=[0-9])\d{1,10}$
that might work.
Demo

Related

regular expression for decimal value greater than 0 but less than 100 [duplicate]

This question already has answers here:
Regex to allow decimal numbers greater than 0 and less than 100
(3 answers)
Regular Expression to accept all the decimals from 0 to 100
(7 answers)
how to match a number which is less than or equal to 100?
(7 answers)
Closed 4 years ago.
I'm trying to write a regular expression to validate a form field that is allowed to have a decimal data type, up to 8 digits long, that is greater than 0 but less than 100. The following seems to be close, although it permits zero and that is where I'm stuck. What should be modified to prevent zero from matching?
^([0-9]{1,2})(\.[0-9]{0,8})?$
Use a positive look behind
^([1-9]?(?<=[1-9])[0-9]?)(\.[0-9]{0,8})?$
learn more here https://www.regular-expressions.info/lookaround.html
and use this to play around with
https://regex101.com/

Regular Expression : Finding the last 9 digits of a number and if the number is less than 9 digits than take the entire number [duplicate]

This question already has answers here:
Using explicitly numbered repetition instead of question mark, star and plus
(4 answers)
Closed 4 years ago.
I have been working on an regular expression which say consists of more than 9 digits ( 12345678910111213 )
With the help of regex \d{9}(?!\d) i am able to find the last 9 digits of the number.
But when the entire number is less than 9 digits how do i take the entire number as pass it. is there any such regular expression.
\d{1,9}(?!\d) will take as many as possible from the end, up to 9.

Validate time and allow negative values [duplicate]

This question already has answers here:
How do I include negative decimal numbers in this regular expression?
(17 answers)
Closed 6 years ago.
I use this regex to validate time entries, e.g. 08:34 - this is a valid time,
negative time e.g. -08:54 is not valid with this regEx.
Actually I don't know how to extend this regex in order to make also negative times valid:
var regex_time =/^([0-9]{2})\:([0-9]{2})$/;
You can use the regex:
^-?([01]\d|2[0-3]):([0-5]\d)$
To get a positive/negative valid 24h format. If you don't need to limit the user to 24h format, use:
^-?(\d+):([0-5]\d)$
The question mark is a quantifier that means 0 to 1 of previous item.

is it possible to do Math on Variable Values in Regex? [duplicate]

This question already has answers here:
Math operations in regex
(3 answers)
Closed 8 years ago.
i am wondering of it is possible to do some simple Math on RegEx Variable values.
E.G.:
I am looking for all two-digit numbers is a textfile and would like to multiply them by 10.
Is simple regex able to do this or do i need to use a more complex script for that?
thanks!
Multiply two-digits number is like appending 0 at the end of the numbers. So that can be done with any regular expression that support replace and capturing group.
For example, here is Python code:
>>> re.sub(r'\b(\d{2})\b', r'\g<1>0', 'There are 10 apples.')
'There are 100 apples.'
But what you want is multiply by arbitrary number, then you need the regular expression engine that support some kind of callback / evaluation.
>>> re.sub(r'\b(\d{2})\b', lambda m: str(int(m.group(1)) * 5), '10 apples.')
'50 apples.'

Regex for range 1-1000 [duplicate]

This question already has answers here:
Regular expression where part of string must be number between 0-100
(7 answers)
Closed 1 year ago.
I need help creating a simple regex for a whole number range of 1-1000, with no special characters.
The two I have both seem to break or allow characters or not the full range:
^\d(\d)?(\d)?$
^[0-9]{1,3}$
Try this:
^([1-9][0-9]{0,2}|1000)$
[1-9][0-9]{0,2} matches any number between 1–999
1000 matches 1000
Use ^(.*[^0-9]|)(1000|[1-9]\d{0,2})([^0-9].*|)$ which will match 1000 or a non-zero digit followed by up to two further digits. It will also allow other characters on either end of the number.