Regex: how to only allow integers greater than zero - regex

I have tried the following to only allow integers in my text box, this works great but it allows a zero in there. Is there anything else I can add to prevent a zero being added?
\d+

This will allow 10 but not 01, and it will allow only numbers consisting of digits, i.e., no periods or minus signs...but also no plus signs, scientific notation etc.
^[1-9][0-9]*$

A minor variation is this:
/\d*[1-9]\d*/
That would allow leading zeros.

If you are not concerned about negatives and silly numbers like 07, this will do:
/[1-9]\d*/
For a more robust solution, I suggest converting the matched string to integer and check if it fulfills your criteria.

Code:
^([1-9][0-9]+|[1-9])$
Example: http://regexr.com/3annd
Tested with:
0
10
01
11
00
1
100

^(0*[1-9][0-9]*)$
This will allow "silly" numbers like 007 as well, but not 0 or 000 or an empty string.
Note that \d matches also digits from other character sets like ٠١٢٣٤٥٦٧٨٩. See: \d is less efficient than [0-9].
^ denotes the start, $ the end of the string. Together they ensure that the whole string is matched.

^(\d{2}[1-9])$
matches with:
from 001 to 999
example
001
099
999
does not match
000
01
0

Related

Regex for all strings over L={0,1} ending with an even number of 0s

sorry for the novice question by I've looked and can't seem to find a question that addressed this.
I want a regex that describes all strings over L={0,1} ending with an even number of 0s.
Examples: 00, 0000, 100, 0100, 001100... basically anything starting with 0 or 1 and ending with an even number of 0s
This is what I've got so far: ((0|1)*1)00+ but this doesn't allow me to get 00 since that must be a 1 always. I can't find a way to put as many 0s as I want at the beginning without having to put that 1.
Thanks a lot.
You could write the pattern as:
^([01]*1)?(00)+$
^ Start of string
( Capture group
[01]*1 Match zero or more repetitions of either 0 or 1 followed by matching 1
)? Close the group and make it optional using ?
(00)+ Match one or more repetitions of 00
$ End of string
See a Regex demo.
If supported, you can also use non capture groups (?:
An even number of 0s is (00)*. It needs to be at the end, so that part of the regex will be (00)*$.
What precedes that even number of 0s? Either nothing or an arbitrary sequence of 0s and 1s ending with a 1. So that's (|[01]*1).
Putting it together, we have:
^(|[01]*1)(00)*$
(I'm assuming extended regex syntax, where (, ), and | don't have to be escaped. Adjust the syntax as needed.)
I have not tested this.

regular expression to identify certain number pattern

I want to build a regular expression to identify certain number pattern
The expressions required would be:
1)
6 numbers, starting with 00
2)
6 numbers, starting with 01
3)
8 numbers, starting with 200.
I started with ^\d{0,6}(.\d{00})?$ bit it did not work
How can it be done?
Try this:
^(0[01][0-9]{4}|200[0-9]{5})$
Will match 0 followed by a 0 or 1 followed by 4 numbers 0-9 (total 6 digits), or it will match 200 followed by 5 digits (total 8 digits)
(Using character groups, due to the fact that the language was not specified, therefore, whether the special characters need extra escapes is unknown)
I think you are looking for the alternation operator |. It takes either the pattern to the left or the pattern to the right. Hence, you end up with the following regular expression:
^(00\d{4}|01\d{4}|200\d{5})$
How about this: a number starting with either 200 and a number, 00 or 01 and 4 numbers
(200\d|00|01)\d{4}

Regex, numbers below 20k

I'm looking for a regex to validate if numbers are below 20 000.
I can't find the right solution, I have so far this:
(^([1-9]([0-9]{0,3})|20000)$)
Which works quite ok but as soon as it gets to 10 000 it gives no matches. So I have a gap from 9 999 - 20 000.
What am I doing wrong? I don't use regex for these situations, but the 3th party program required regex for such..
Thanks!
Your regex - ^([1-9]([0-9]{0,3})|20000)$ - matches numbers from 1 till 9999 and 20000.
You may use
^([1-9]\d{0,3}|1\d{4}|20000)$
See demo
Breakdown:
^ - match start of string
([1-9]\d{0,3}|1\d{4}|20000) - match one of the alternatives:
[1-9]\d{0,3} - 1 to 9 followed with 0 to 3 any digits (from 1 till 9999)
1\d{4} - 1 followed with any 4 digits (to match 10000 - 19999)
20000 - literally 20000
$ - match the end of string
I've got this:
^([01]?\d{0,4}|20000)$
Which match any number from 0 to 20 000 and allow the user to use number with leading 0 Live Demo
The ([1-9]([0-9]{0,3}) part is designed to match all numbers strictly below 2000 but you define it as: "A digit one to nine followed by zero to three digits". Now 10 000 is a one followed by four zeros: you can rewrite the part as:
[1-9][0-9]{3}|1[0-9]{4}
The full regex is now:
^[1-9][0-9]{3}|1[0-9]{4}|20000$

Regex for currency number, How can I write it shorter?

^((?=.*[1-9]|0)(?:\d{1,3}))((?=.*\d)(?:\.\d{3})?)*((?=.*\d)(?:\,\d\d){1}?){0,1}$
I actually think this regular expression is very long, and mayby could be shorter. The problem is i'm not very good with regular expressions and therefore I ask you for help.
Online regex tester http://regexr.com/3a3mk
My rules:
Starting with 1, 2 or 3 positive numbers [1-9] or 0.
Adding as many . (followed by 3 numbers [0-9]) as you want.
Possibility to add a comma with 2 numbers (as decimals)
Positive results
0
0,55
1
1,60
10
10,70
100
100,80
1,10
1.000
1.000,20
10.000
10.000,03
100.000
100.000,08
1.000.000.000
1.000.000.000,10
Negative results
0,0
1,1
1,000
1000.000
0.000
0.000,10
1.000,1
1.000,100
1.0,00
1.00,00
1.000,0
01
012,10
012.123,10
a
a0
0,a
0,aa
1.a00.00
1.000.a1
[EDIT] Added more negative results
The following should suit your needs:
^(?:0|[1-9]\d{0,2})(?:\.\d{3})*(?:,\d{2})?$
Visualization by Debuggex
Demo on regex101
Edited:
^(0|[1-9][0-9]{0,2}(\.[0-9]{3})*)(,[0-9]{2})?$
matches:
^ beginning of line
[1-9] just one non-zero digit
[0-9]{0,2} between 0 and 2 digits
(\.[0-9]{3})* zero or more lots of a period and 3 digits
(0 | [1-9][0-9]{0,2}(\.[0-9]{3})*) either (i) a zero or (ii) up to three digits (the first not a zero) followed by blocks of zero or more lots of a period followed by three digits
(,[0-9]{2})? zero or one lots of a comma and 2 digits
$ end of line
You'Re right, that your expression is a bit to long. A shorter version that works with the example numbers and specifications you gave would be this:
^(0|\d{1,3})(\.\d{3})*(,\d{2})?$
Explanation:
(0|\d{1,3}) checks for a 0 or 1 to 3 digits
(\.\d{3})*checks for a dot and 3 numbers, but because of *there can also be none of them
(,\d{2})? ckecks for a comma and two digits, but again it can appear once or not at all.
Hope it helps you!

Regex allow a string to only contain numbers 0 - 9 and limit length to 45

I am trying to create a regex to have a string only contain 0-9 as the characters and it must be at least 1 char in length and no more than 45. so example would be 00303039 would be a match, and 039330a29 would not.
So far this is what I have but I am not sure that it is correct
[0-9]{1,45}
I have also tried
^[0-9]{45}*$
but that does not seem to work either. I am not very familiar with regex so any help would be great. Thanks!
You are almost there, all you need is start anchor (^) and end anchor ($):
^[0-9]{1,45}$
\d is short for the character class [0-9]. You can use that as:
^\d{1,45}$
The anchors force the pattern to match entire input, not just a part of it.
Your regex [0-9]{1,45} looks for 1 to 45 digits, so string like foo1 also get matched as it contains 1.
^[0-9]{1,45} looks for 1 to 45 digits but these digits must be at the beginning of the input. It matches 123 but also 123foo
[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be at the end of the input. It matches 123 but also foo123
^[0-9]{1,45}$ looks for 1 to 45 digits but these digits must be both at the start and at the end of the input, effectively it should be entire input.
The first matches any number of digits within your string (allows other characters too, i.e.: "039330a29"). The second allows only 45 digits (and not less). So just take the better from both:
^\d{1,45}$
where \d is the same like [0-9].
Use this regular expression if you don't want to start with zero:
^[1-9]([0-9]{1,45}$)
If you don't mind starting with zero, use:
^[0-9]{1,45}$
codaddict has provided the right answer. As for what you've tried, I'll explain why they don't make the cut:
[0-9]{1,45} is almost there, however it matches a 1-to-45-digit string even if it occurs within another longer string containing other characters. Hence you need ^ and $ to restrict it to an exact match.
^[0-9]{45}*$ matches an exactly-45-digit string, repeated 0 or any number of times (*). That means the length of the string can only be 0 or a multiple of 45 (90, 135, 180...).
A combination of both attempts is probably what you need:
^[0-9]{1,45}$
^[0-9]{1,45}$ is correct.
Rails doesnt like the using of ^ and $ for some security reasons , probably its better to use \A and \z to set the beginning and the end of the string
For this case word boundary (\b) can also be used instead of start anchor (^) and end anchor ($):
\b\d{1,45}\b
\b is a position between \w and \W (non-word char), or at the beginning or end of a string.