Can I get the regular expression for the following criteria?
Number format only
If number starts with 07 then min length = 11 and max length = 11.
If number starts with 01, 02, 03, 04, 05, 06, 08, 09 then min length = 10 and max length = 11.
I tried this but doesnt seem to be working.
^[0]{1}([0-6,8-9]{10,11}|[7]{11})$
I think you need this regex pattern:
^(?:07\d{9}|0[1-689]\d{8,9})$
or equivalent
^(?:07[0-9]{9}|0[1-689][0-9]{8,9})$
Explanation:
07\d{9} = 0, 7, and exactly 9 digits
0[1-689]\d{8,9} = 0, 1, any digit but 7, and 8 or 9 digits after.
In your regex, you specify [7]{11} = 7 digit repeated 11 times, [0-6,8-9]{10,11} meaning "any digit but 7 or a comma 10 or 11 times". All that prevents matching all digits where you need them. The length restriction should also take into account the number of already matched digits.
Related
How do I create a regular expression for this rule?
I should only accept this number if these rules are followed.
Starts with 03 or 04, 10 digits long
Starts with 3 or 4, 9 digits long
Edit:
So far, I have only this.
/^[0-9]{9,10}$/
It can only accept numbers with 9 or 10 digits. The starting digits however are still from 0 to 9. It should only be specific, 3 or 4 for 9 digit number, 03 or 04 for 10 digit number. The rule should be one line, applicable to both rules.
This should work
/^0?[34][0-9]{8}$/
You can try below regex:
^(03|04)[1-9]{8}$ -> for Starting by 03 or 04 , 10 digits long
Demo Here
/^(3|4)[0-9]{8}$/gm -> Starting by 3 or 4 , 9 digits long
Demo Here
I am trying to match various months, that may be in the form of:
01
1
12
13
09
All of the above inputs are valid except for 13.
The current regex I have for this is:
0?(?#optional leading 0, for example 04)
\d(?#followed by any number, 01, 2, 09, etc.)
|(?#or 10,11,12)
1[012]
What's wrong with the above regex? Here's an example link: https://regex101.com/r/cujCmD/1
I would phrase the regex as:
^(?:0?[1-9]|1[012])$
Demo
The parentheses and anchors are needed to ensure that the alternation chosen gets applied to the entire number input.
I'm trying to get a regex to get only even numbers and the highest number that can be entered should be 12.
I got this to work for only even number:
^(\d*[02468])$
Now I just need help with getting the highest number that can be entered is 12. I tried:
^(\d*[02468]|[0-1][0-2])$
but that didn't work. Any suggestions?
Your ^(\d*[02468]|[0-1][0-2])$ regex matches (with \d*[02468]) 0 or more digits followed with 0, 2, 4, 6 (so, it can match 32) or (with [0-1][0-2]) 8 or 00, 01, 02, 10, 11 or 12. As you see, they are not all even.
You may use
^([02468]|1[02])$
Or with a leading optional 0:
^(0?[02468]|1[02])$
See the regex demo
Details:
^ - start of string
( - Grouping construct matching either
[02468] - a digit from the set
| 0- or
1[02] - 10 or 12
) - end of group
$ - end of string
Just list all the valid numbers: ^(0|2|4|6|8|10|12)$. It's not the shortest solution but the most easy to read and understand.
In an application I have the need to validate a string entered by the user.
One number
OR
a range (two numbers separated by a '-')
OR
a list of comma separated numbers and/or ranges
AND
any number must be between 1 and 999999.
A space is allowed before and after a comma and or '-'.
I thought the following regular expression would do it.
(\d{1,6}\040?(,|-)?\040?){1,}
This matches the following (which is excellent). (\040 in the regular expression is the character for space).
00001
12
20,21,22
100-200
1,2-9,11-12
20, 21, 22
100 - 200
1, 2 - 9, 11 - 12
However, I also get a match on:
!!!12
What am I missing here?
You need to anchor your regex
^(\d{1,6}\040?(,|-)?\040?){1,}$
otherwise you will get a partial match on "!!!12", it matches only on the last digits.
See it here on Regexr
/\d*[-]?\d*/
i have tested this with perl:
> cat temp
00001
12
20,21,22
100-200
1,2-9,11-12
20, 21, 22
100-200
1, 2-9, 11-12
> perl -lne 'push #a,/\d*[-]?\d*/g;END{print "#a"}' temp
00001 12 20 21 22 100-200 1 2-9 11-12 20 21 22 100-200 1 2-9 11-12
As the result above shows putting all the regex matches in an array and finally printing the array elements.
I have a date entry control on a UI. It auto-completes the date as numbers are typed.
Currently using:
\d{1,2}(\.|\/|-)\d{1,2}
Test on http://regexpal.com/ to see it working.
Basically 1 or 2 digits, followed by one of ./-, and 1 or 2 digits. e.g. 01/9, 1/2, 1/1, will match
What I'm looking to do (and can't!):
If the second set of digits (the Month part) is 0 or 1, need another digit
If the second set of digits is 2-9, match
That's it basically, thanks
Brian
Number ranges are a bit annoying in regex, and should maybe be done in a separate step, when the format is validated. But here is the regex:
\d{1,2}[/.-](0?[2-9]|1[0-2]|01|1[/.-])
That should match all the requirements (including the ones from the comments). Possible months:
2, 3, ..., 9
01, 02, 03, ..., 09
10, 11, 12
1/, 1., 1-