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.
Related
I have a set of age data, like below;
1
2
3
4
5
6
7
8
9
10
1,1
1,2
1,3
2,12
11,13,15
7,8,12
12,15
14,16,17
15,6
13,11,10,2
And so on... I am trying to use Regex in to target a 'mixed' range of childrens ages. The logic requires at least a combination of 2 childen (so requires one of the lines with a comma), with at least one aged under 10 (min is 1), and at least one aged equal or greater to 10 (max 17).
My expected results from the above would be to return these lines below, and nothing else;
2,12
7,8,12
15,6
13,11,10,2
Any advice would be appreciated on how to resolve? Thanks in advance, I am continuing to try to correct.
You can use this regex to meet your requirements:
^(?=.*\b[1-9]\b)(?=.*\b1[0-7]\b)[0-9]+(?:,[0-9]+)+$
RegEx Demo
There are 2 lookaheads to assert 2 numbers one between 1-9 and another between 10-17
([1-9]) matches a number that should be between 1 and 9
1[0-7] matches a number that should be between 10 and 17
[0-9]+(?:,[0-9]+)+ in the regex is for matching 1 or more comma separated numbers in the middle.
You can do it with
\b\d,1[0-7]\b
provided the ages always are sorted (youngest to oldest).
If the age of 0 isn't allowed, change to
\b[1-9],1[0-7]\b
It checks for a single digit followed by a comma and one followed by a single digit in the range 0-7.
See it here at regex101.
I am trying to write a regex that will match Roman numerals from 0 to 39 only. There are plenty of examples which match much larger Roman numerals, but I cannot figure out how to match this specific subset.
Got it. Try this:
/^(X{1,3})(I[XV]|V?I{0,3})$|^(I[XV]|V?I{1,3})$|^V$/
Update:
Zero doesn't exist in Roman numerals. Therefore feel free to tack on your own implementation for zero.
I'm not sure how to represent 0 using Roman numerals. I assume that it has separate token N (see Wikipedia).
Assuming the regex tries to match the whole string (like in Java) and you have lookahead, you can use this regex:
(?.)(X{0,3}(IX|IV|V?I{0,3})|N)
Explanation:
(?.): ensure at least one character
X{0,3}: define the tens (0, 10, 20, 30)
(...): define the final digit
IX: 9
IV: 4
V?I{0,3}: 0-3, 5-8 (0 not as whole number, require at least one X)
N: 0 (as whole number)
If you represent 0 as empty string, the regex is simpler:
X{0,3}(IX|IV|V?I{0,3})
since the lookahead and N in the previous regex is just to prevent empty string.
Assuming you know you have valid Roman numerals and want to fetch only the ones <= 39, that is easy:
^[XVI]*$
See it in action
If that is not the case, it's a little bit trickier, but you can still take advantage of the fact that all the numbers that can be represented only with X, V and I are 1..39:
^X{0,3}(?:V?I{0,3}|I[VX])$
See it in action
X{0,3} covers 10, 20, 30
X{0,3}V?I{0,3} covers all but the ones that end with 4 or 9 (14, 29, etc)
X{0,3}I[VX] exactly the ones ending with 4 or 9
Note: these will also match an empty string, which is my interpretation of a Roman zero. If that is not the case, you can replace the * with + for the first regex and add a positive lookahead at the start of the regex for the second ((?=.)).
Note 2: If they are not on separate lines (or in separate strings), you can replace ^ and $ with word boundaries (\b).
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.
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 regex that looks like this
((1010xxx)?(\d{11}|\d{10}|\d{7})+)
Basically I want it to match
8085551234
5551234
10102338085551234
and it should fail on
1010233
This is more for validation being done on an xsd than an actual matcher.
PS.
I am trying to match US telephone numbers 7 - 11 digits long with an optional 1010xxx at the front. Also if it is 1010xxx it should not pass. xxx is any 3 digits.
If all you want is to make sure that it's a 7, 10, or 11 digit string, making sure that if it's only 7 digits it doesn't start with '1010', you can use a negative lookahead assertion before your match on \d{7}, i.e.:
((\d{11}|\d{10}|(?!1010)\d{7})+)