Regular expression for numbers without leading zeros - regex

I need a regular expression to match any number from 0 to 99. Leading zeros may not be included, this means that f.ex. 05 is not allowed.
I know how to match 1-99, but do not get the 0 included.
My regular expression for 1-99 is
^[1-9][0-9]?$

There are plenty of ways to do it but here is an alternative to allow any number length without leading zeros
0-99:
^(0|[1-9][0-9]{0,1})$
0-999 (just increase {0,2}):
^(0|[1-9][0-9]{0,2})$
1-99:
^([1-9][0-9]{0,1})$
1-100:
^([1-9][0-9]{0,1}|100)$
Any number in the world
^(0|[1-9][0-9]*)$
12 to 999
^(1[2-9]|[2-9][0-9]{1}|[1-9][0-9]{2})$

Updated:
^([0-9]|[1-9][0-9])$
Matches 0-99. Doesn't match values with leading zeros. Depending on your application you may need to escape the parentheses and the or symbol.

^(0|[1-9][0-9]?)$
Test here http://regexr.com?2uu31 (various samples included)
You have to add a 0|, but be aware that the "or" (|) in Regexes has the lowest precedence. ^0|[1-9][0-9]?$ in reality means (^0)|([1-9][0-9]?$) (we will ignore that now there are two capturing groups). So it means "the string begins with 0" OR "the string ends with [1-9][0-9]?". An alternative to using brackets is to repeat the ^$, like ^0$|^[1-9][0-9]?$.

[...] but do not get the 0 included.
Just add 0|... in front of the expression:
^(0|[1-9][0-9]?)$
^^

console.log(/^0(?! \d+$)/.test('0123')); // true
console.log(/^0(?! \d+$)/.test('10123')); // false
console.log(/^0(?! \d+$)/.test('00123')); // true
console.log(/^0(?! \d+$)/.test('088770123')); // true
How about this?

A simpler answer without using the or operator makes the leading digit optional:
^[1-9]?[0-9]$
Matches 0-99 disallowing leading zeros (01-09).

This should do the trick:
^(?:0|[1-9][0-9]?)$

Answer:
^([1-9])?(\d)$
Explanation:
^ // beginning of the string
([1-9])? // first group (optional) in range 1-9 (not zero here)
(\d) // second group matches any digit including 0
$ // end of the string
Same as (Not grouping):
^[1-9]?\d$
Test:
https://regex101.com/r/Tpe9Ia/1

Try this it will help you
^([0-9]|[1-9][0-9])$

([1-9][0-9]+).*
this will be simple and efficient
it will help with any range of whole numbers
([1-9][0-9\.]+).*
this expression will help with decimal numbers

You can use the following regex:
[1-9][0-9]\d|0

^(0{1,})?([1-9][0-9]{0,1})$
It includes:
1-99,
01-099,
00...1-

Related

*NIX REGEXP number series

Am playing around with regexp's but this is my headache. I have a dynamic number which needs a suffix. The suffix is always 0 to 9, 99 or 999.
Example:
I have the number 461200 and now I want to create an regexp that will match 461200 to 461209. What I've learned it should be ^46120[0-9]$? Is this correct or somewhere to the left of hell?
Ok, let us assume it is correct and I now want to match 461200 - 461299? This is where I get lost.
^4612[0-9]{2}?
It cannot be. I am yet to figure this out.
Any help appreciated.
For 1 digit at the end you need:
^4612[0-9]$
2 digits at the end:
^4612[0-9]{2}$
3 digits at the end:
^4612[0-9]{3}$
The number in braces {} means the number of time the preceding character or set has to be repeated.
Ok, let us assume it is correct and I now want to match 461200 -
461299?
You can either repeat the desired character class by saying [0-9][0-9] or use quantifiers [0-9]{2}.
It can be either:
^4612[0-9][0-9]$
or
^4612[0-9]{2}$
Both would work.
maybe try this regex:
^4612\d{2}$

I need a single regex to check number comma and number

I need a regX which can match like 123,123 only. My regX is
var regX = /^\d*[0-9](|.\d*[0-9]|,\d*[0-9])*$/;
but its currently matching 123,123 and 123, as well
Valid case: 123,123 or 123,000 or 000,000
Invalid case: 123.123 or 123?123 or '123'.'123'
you should use this regex = \d+(,\d+)+
You might want to use the {x,y} quantifier. I matches at least X of the item, and at most Y. If you leave one out, it has no limit in that direction. If you just have one number, with no comma it matches exactly that amount.
Exactly three digits:
(\d{3}),(\d{3})
Three or more
(\d{3,}),(\d{3,})
Between 2 and 7 digits:
(\d{2,7}),(\d{2,7})
And so on...
It looks like you're actually trying to match a number with thousand separators.
Try this: /\d{1,3}(?:,\d{3})*/
If your numbers are positive integers you can use: \d+,\d+
If you want floating point numbers as well: (\d|.)+,(\d|.)+
although this will also match malformed numbers with multiple or misplaced decimal points including .,. etc.

Regular Expression for a 0.25 interval

My aim is to write a regular expression for a decimal number where a valid number is one of
xx.0, xx.125, xx.25, xx.375, xx.5, xx.625, xx.75, xx.875 (i.e. measured in 1/8ths) The xx can be 0, 1 or 2 digits.
i have come up with the following regex:
^\d*\.?((25)|(50)|(5)|(75)|(0)|(00))?$
while this works for 0.25,0.5,0.75 it wont work for 0.225, 0.675 etc .
i assumed that the '?' would work in a case where there is preceding number as well.
Can someone point out my mistake
Edit : require the number to be a decimal !
Edit2 : i realized my mistake i was confused about the '?'. Thank you.
I would add another \d* after the literal . check \.
^\d*\.?\d*((25)|(50)|(5)|(75)|(0)|(00))?$
I think it would probably just be easier to multiply the decimal part by 8, but you don't consider digits that lead the last two decimals in the regex.
^\d{0,2}\.(00?|(1|6)?25|(3|8)?75|50?)$
Your mistake is: \.? indicates one optional \., not a digit (or anything else, in this case).
About the ? (question mark) operator: Makes the preceding item optional. Greedy, so the optional item is included in the match if possible. (source)
^\d{0,2}\.(0|(1|2|6)?25|(3|6|8)?75|5)$
Regular expressions are for matching patterns, not checking numeric values. Find a likely string with the regex, then check its numeric value in whatever your host language is (PHP, whatever).

Regex that matches integers in between whitespace or start/end of string only

I'm currently using the pattern: \b\d+\b, testing it with these entries:
numb3r
2
3454
3.214
test
I only want it to catch 2, and 3454. It works great for catching number words, except that the boundary flags (\b) include "." as consideration as a separate word. I tried excluding the period, but had troubles writing the pattern.
Basically I want to remove integer words, and just them alone.
All you want is the below regex:
^\d+$
Similar to manojlds but includes the optional negative/positive numbers:
var regex = /^[-+]?\d+$/;
EDIT
If you don't want to allow zeros in the front (023 becomes invalid), you could write it this way:
var regex = /^[-+]?[1-9]\d*$/;
EDIT 2
As #DmitriyLezhnev pointed out, if you want to allow the number 0 to be valid by itself but still invalid when in front of other numbers (example: 0 is valid, but 023 is invalid). Then you could use
var regex = /^([+-]?[1-9]\d*|0)$/
You could use lookaround instead if all you want to match is whitespace:
(?<=\s|^)\d+(?=\s|$)
This just allow positive integers.
^[0-9]*[1-9][0-9]*$
I would add this as a comment to the other good answers, but I need more reputation to do so. Be sure to allow for scientific notation if necessary, i.e. 3e4 = 30000. This is default behavior in many languages. I found the following regex to work:
/^[-+]?\d+([Ee][+-]?\d+)?$/;
// ^^ If 'e' is present to denote exp notation, get it
// ^^^^^ along with optional sign of exponent
// ^^^ and the exponent itself
// ^ ^^ The entire exponent expression is optional
This solution matches integers:
Negative integers are matched (-1,-2,etc)
Single zeroes are matched (0)
Negative zeroes are not (-0, -01, -02)
Empty spaces are not matched ('')
/^(0|-*[1-9]+[0-9]*)$/
^([+-]?[0-9]\d*|0)$
will accept numbers with leading "+", leading "-" and leadings "0"
Try /^(?:-?[1-9]\d*$)|(?:^0)$/.
It matches positive, negative numbers as well as zeros.
It doesn't match input like 00, -0, +0, -00, +00, 01.
Online testing available at http://rubular.com/r/FlnXVL6SOq
^(-+)?[1-9][0-9]*$
starts with a - or + for 0 or 1 times, then you want a non zero number (because there is not such a thing -0 or +0) and then it continues with any number from 0 to 9
This worked in my case where I needed positive and negative integers that should NOT include zero-starting numbers like 01258 but should of course include 0
^(-?[1-9]+\d*)$|^0$
Example of valid values:
"3",
"-3",
"0",
"-555",
"945465464654"
Example of not valid values:
"0.0",
"1.0",
"0.7",
"690.7",
"0.0001",
"a",
"",
" ",
".",
"-",
"001",
"00.2",
"000.5",
".3",
"3.",
" -1",
"+100",
"--1",
"-.1",
"-0",
"00099",
"099"

Simple phone regex

I want a regex that checks the following things:
The string starts with an +
After the '+' only numbers can occur
There should be atleast 4 numbers after the +
Does anyone know how to make this?
/^+\d{4,}$/
will meet your requirements.
^ is the anchor for start fo the string
\d is a digit
{4,} says at least 4 of the preceding expression (here the \d). you can add a maximum if needed like {4,20} would allow at least 4 and at most 20 characters.
$ is the anchor for the end of the string
/^((00|\+)[0-9]{2,3}){0,1}[0-9]{4,14}$/
More general than your request, but you can specialize it. Explaining:
((00|\+)[0-9]{2,3})
international code with 00 or + and 2 or 3 digits. Modify the expression according to your needs.
{0,1}
international code is optional - remove it if it is required
[0-9]{4,14}
digits: minimum 4, maximum 14. Change the values according to your needs.
Regards
A.
/\+\d{4,15}/
This should help if 15 is the atmost limit of numbers
OR rather keep the second parameter blank as stema suggested.
I went with this one:
/\A(([+]\d{3,})?\d{6,8})/