Regex to allow only number between 1 to 12
I am trying (12)|[1-9]\d? but its not working, please help as i am new to regular expression
Something like
^([1-9]|1[012])$
^ Anchors the regex at start of the string
[1-9] Matches 1 to 9
| Alternation, matches the previous match or the following match.
1[012] Matches 10, 11, or 12
$ Anchors the regex at the end of the string.
Regex Demo
Here's some readymade regex expressions for a bunch of different numbers within a certain range:
Range
Label
Regex
1 to 12
hour / month
1[0-2]|[1-9]
1 to 24
hour
2[0-4]|1[0-9]|[1-9]
1 to 31
day of month
3[01]|[12][0-9]|[1-9]
1 to 53
week of year
5[0-3]|[1-4][0-9]|[1-9]
0 to 59
min / sec
[1-5]?[0-9]
0 to 100
percentage
100|[1-9]?[0-9]
0 to 127
signed byte
12[0-7]|1[01][0-9]|[1-9]?[0-9]
32 to 126
ASCII codes
12[0-6]|1[01][0-9]|[4-9][0-9]|3[2-9]
Try something like this:
^(1[0-2]|[1-9])$
1[0-2] : first charcter must be 1 and second character can be in range
from 0 to 2
[1-9] : numbers from 1-9
^ : start of string
$ : end of string
Demo
[1-9]|1[012] works for greedy quantifiers (that try to match as much as they can) but will not match 10 for lazy quantifiers because as soon as it sees 1 it will stop.
Try this at https://regex101.com/
[2-9]|1[012] will work with lazy quantifiers
I think this should work
\[1-9]|1[0-2]\
Related
I have a regular expression like:
/^([0-9]{2,3})/
This will accepts 2 or 3 number digits between 0 and 9
123 or 12
I need a validate to: if the number has 3 digits, the first should be 0, in our case 023
and if not, number should be the 2 digits one: 12
Can anyone help me?
You may use
^0?\d{2}$
See the regex demo
Details
^ - start of string
0? - an optional 0
\d{2} - two digits
$ - end of string
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$
I want to write a regular expression on Google Form
First Character between 1 to 9
Second and Third any alphabets (Upper Case)
and next 3 characters should be number like 541 or 001 but not 000
This expression is also taking 000
[1-9][A-Z]{2}[0-9]{3}
Use alternations:
[1-9][A-Z]{2}([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9])
See regex demo
Here,
[1-9] - matches 1 digit from 1 to 9
[A-Z]{2} - two uppercase ASCII letters
([1-9][0-9][0-9]|[0-9][1-9][0-9]|[0-9][0-9][1-9]) - 3 alternatives:
[1-9][0-9][0-9] - 3-digit numbers starting with 1
[0-9][1-9][0-9] - 3-digit numbers having 1 in the middle
[0-9][0-9][1-9] - 3-digit numbers ending with 1
Also, see this regex demo.
Use a negative look-ahead to avoid the triple zero at the end:
[1-9][A-Z]{2}(?!000)[0-9]{3}
Using the alternation operator
[1-9][1-9][1-9]|0[1-9][1-9]|00[1-9]|0[1-9]0
I need to check for a link where title="Week 1" or title="Week 13", I have tried somethings what I found on the internet but it won`t work:
http://regexr.com?38bet
So, how to match if something have one number or two numbers?
You can use \d{1,2} This will match between 1 and 2 digits.
I have also found that the following this website is very helpful when I need to write regexes.
Use a zero-or-one quantifier (?), like this:
title="[Ww]eek [1-9][0-9]?"
This will match a string which contains title=" followed by an upper- or lower-case w, followed by eek, a space, and digit from 1 to 9, followed by an optional digit from 0 to 9, followed by a ".
You should also consider using the case-insensitive flag in the language of your choice.
The answer depends on which context you're using the regular expression.
In python, ? will match 0 or 1 characters.
In JavaScript, ? will also match 0 or 1 characters.
In vi, \= will match 0 or 1 characters.
So the answer(s) would be:
Python regex: Week 13?
JavaScript regex: /Week 13?/
vi regex: Week 13\=
Which will match both:
Week 1
Week 13
for java try this one:
String regex = "[\\d{2}]+";
Try this :
title="[Ww]eek (\d+)\.pdf"
RegExr :
http://regexr.com?38bfc
Matches :
1
13
I need help with a regex
I need it to match either a 9 or 10 digit value that starts with 50.
I have:
^[ ]*(50)[0-9]{7}[ ]*$
which allows 9 digits.
How can I expand this so that it also allows 10 digits?
Add the range {7,8}
^[ ]*(50)[0-9]{7,8}[ ]*$
FYI this site describes the standard quantifiers that you can use in a regular expression:
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
Try with following regex:
^[ ]*50\d{7,8}[ ]*$
This regex will match what you need:
^\s*50\d{7,8}\s*$
This will match all 9 or 10 digit numbers starting with 50 with an unlimited number of spaces before, or after, them on the line.
If you want to match all 9 or 10 digit numbers starting with 50 regardless of position and number of spaces etc then:
50\d{7,8}
will do exactly what you need.
Here's what you need: (50)\d{7,8}