Regex to match even, non-zero numbers - regex

I have the following regex: [\-+]?[0-9]*[02468]$
which matches positive or negative even numbers, but I also want it to not match '0'. How can I accomplish this? I can't find a way to translate "Only match 0 as the final number if there are numbers that precede it" into regex language.
Thanks.

Maybe this one?
[\-+]?([0-9]*[1-9][0-9]*[02468]|[2468])$
EDIT
This allows leading zeroes on both alternations.
[-+]?0*([1-9][0-9]*[02468]|[2468])$

I came up with the following, which seems to work: [-+]?([1-9][0-9]*[24680]|[2468])$
It appears to avoid leading zeros and doesn't use lookback. Correct me if I am wrong.

This would eliminate numbers that start with 0:
[\-+]?[^0][0-9]*[02468]$
So it would exclude 0.
The drawback is it wouldn't work for 012.
So it could be a solution as long as your numbers don't have leading zeros.
I don't understand the need for a regex here, though (you never specify the context).
It would be better (if possible) to check if the number is non-zero and even:
( num != 0 && num % 2 == 0 )

Related

Regular expression to check if a string is number including decimal number

I want to check if a string is a number. The accepted range of numbers in my case varies a lot from large numbers with a lot of decimals like;
100000000000000000.000000000000000001
1
25.9897
Above values should be matched!
Values that should not match are;
10,000.4
e19
How can I approach this?
^\d+(\.\d+)?$
A number with any length \d+, then maybe some optional decimal part (\.\d+)?
Also important to utilize line anchors ^ and $ to filter cases like e19
A possible problem can be a value like; 010.5, leading 0s can be kinda problematic, is that acceptable? Otherwise the way to filter values with trailing 0s out is to use; ^[1-9]\d*(\.\d+)?$. Just FYI
see it on regex101

*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}$

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).

Regular expression for numbers without leading zeros

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-

Regex - Find numbers between 2000 and 3000

I have a need to search all numbers with 4 digits between 2000 and 3000.
It can be that letters are before and after.
I thought I can use [2000-3000]{4}, but doesnt work, why?
thank you.
How about
^2\d{3}|3000$
Or as Amarghosh & Bart K. & jleedev pointed out, to match multiple instances
\b(?:2[0-9]{3}|3000)\b
If you need to match a3000 or 3000a but not 13000, you would need lookahead and lookbefore like
(?<![0-9])(?:2[0-9]{3}|3000)(?![0-9])
Regular expressions are rarely suitable for checking ranges since for ranges like 27 through 9076 inclusive, they become incredibly ugly. It can be done but you're really better off just doing a regex to check for numerics, something like:
^[0-9]+$
which should work on just about every regex engine, and then check the range manually.
In toto:
def isBetween2kAnd3k(s):
if not s.match ("^[0-9]+$"):
return false
i = s.toInt()
if i < 2000 or i > 3000:
return false
return true
What your particular regex [2000-3000]{4} is checking for is exactly four occurrences of any of the following character: 2,0,0,0-3,0,0,0 - in other words, exactly four digits drawn from 0-3.
With letters before an after, you will need to modify the regex and check the correct substring, something like:
def isBetween2kAnd3kWithLetters(s):
if not s.match ("^[A-Za-z]*[0-9]{4}[A-Za-z]*$"):
return false
idx = s.locate ("[0-9]")
i = s.substring(idx,4).toInt()
if i < 2000 or i > 3000:
return false
return true
As an aside, a regex for checking the range 27 through 9076 inclusive would be something like this hideous monstrosity:
^2[7-9]|[3-9][9-9]|[1-9][0-9]{2}|[1-8][0-9]{3}|90[0-6][0-9]|907[0-6]$
I think that's substantially less readable than using ^[1-9][0-9]+$ then checking if it's between 27 and 9076 with an if statement?
Hum tricky one. The dash - only applies to the character immediately before and after so what your regex is actually matching is exactly 4 characters between 0 and 3 inclusive (ie, 0, 1, 2 and 3). eg, 3210, 1230, 3333, etc... Try the expression below.
(2[0-9]{3})|(3000)
Here's explanation why and ways to detect ranges: http://www.regular-expressions.info/numericranges.html
Correct regex will be \b(2\d{3}|3000)\b. That means: match character '2' then exactly three digits (this will match any from 2000 to 2999) or just match '3000'. There are some good tutorials on regular expressions:
http://gnosis.cx/publish/programming/regular_expressions.html
http://immike.net/blog/2007/04/06/the-absolute-bare-minimum-every-programmer-should-know-about-regular-expressions/
http://www.regular-expressions.info/
why don't you check for greater or less than? its simpler than a regex
num >= 2000 and num <=3000