RegEx to allow 12 or 13 digits - regex

In my MVC application I define validation using the following RegEx
[RegularExpression(#"\d{8}0[1-2]\d{3}", ErrorMessage = "Must be numeric, 12 or 13 characters long & Format xxxxxxxx[01 or 02]xxx")]
But I want to allow 12 or 13 characters. The d{3} appears to be forcing that overall I have 13 characters input
To allow it to accept 12 or 13, I have changed d{3} to d{2} and its accepting 12 now.
But - can I be sure it will still take 13 characters?

Must be numeric, 12 or 13 characters long & Format xxxxxxxx[01 or 02]xxx
To allow digits 1 or 2 after first nine digits,
^\d{8}0[12]\d{2,3}$
^^^^ : Allow 1 or 2 after `0`
^^^^^^^ : Any two or three digits
Note that [12] can also be written as (1|2) using OR/alteration.
Demo

Related

Regex for this rule

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

Regular Expression

I'm trying to get the regular expression to work (using jQuery) for a specific pattern I need.
I need following pattern:
First two character
s of the string need to be numbers (0-9) but maximum number is 53. for numbers below 10 a leading 0 is required
Character on position 3 needs to be a .
the next 4 characters need to be a number between 0-9, minimum number should be 2010, maximum 2050
so, Strings like 01.2020, 21.2020, or 45.2020 have to match but 54.2020 or 04.2051 must not.
I tried to write the regex without the min and max requirement first and I'm testing the string using regex101.com but I'm unable to get it to work.
acording to the definition /^[0-9]{2}\.\d[0-9]{4}$/ should allow me to insert the strings in the format NN.NNNN.
thankful for any input.
2 numbers from 00 to 53 can be matched using this : (?:[0-4][0-9]|5[0-3]) (00 -> 49 or 50 -> 53)
Character on position 3 needs to be a . : you've already got the \.
a number between 2010 and 2050 -> 20(?:[1-4][0-9]|50) (20 followed by either 10 -> 49 or 50)
This gives :
(?:[0-4][0-9]|5[0-3])\.20(?:[1-4][0-9]|50)

Regexp in oracle to match a string

I am trying to create a RegExp in oracle to match a string with the following criteria,
Length 11 characters.
The 2,5,8,9 characters are letters [A-Z ] except ( S,L, O,I,B and Z).
The 1,4,7,10,11 characters are numeric [0-9].
3rd and 6th will b either a number or a letter.
You'll want to use the following regex with REGEXP_LIKE(), REGEXP_SUBSTR(), etc:
^[0-9][AC-HJKMNP-RT-Y][A-Z0-9][0-9][AC-HJKMNP-RT-Y][A-Z0-9][0-9][AC-HJKMNP-RT-Y]{2}[0-9]{2}$
Hope this helps.
Make a fancy Character List
I just make a fancy character list excluding the alphabetical upper case letter you cited. This is similar to David Faber's answer.
Here is my fancy character list:
-'[AC-HJKMNPQRT-Y]' -Oracle's documentation states that the hyphen is special in that it forms a range when in this character list.
To make this pattern succinct, I noticed that for the most part, this string follows a pattern of digit, alphabet, alphabet pattern. Consequently, I placed this in a subexpression grouping which occurs 2 times (quantifier follows).
SCOTT#db>WITH smple AS (
2 SELECT
3 '123456789ab' tst
4 FROM
5 dual
6 UNION ALL
7 SELECT
8 '1CC4DD7EE01'
9 FROM
10 dual
11 UNION ALL
12 SELECT
13 '1CB4DD7EE01'
14 FROM
15 dual
16 UNION ALL
17 SELECT
18 '1C44D67EE01'
19 FROM
20 dual
21 ) SELECT
22 smple.tst,
23 regexp_substr(smple.tst,'^(\d[AC-HJKMNPQRT-Y](\d|[AC-HJKMNPQRT-Y])){2}\d[AC-HJKMNPQRT-Y]{2}\d{2}$') matching
24 FROM
25 smple;
TST MATCHING
-------------------------
123456789ab
1CC4DD7EE01 1CC4DD7EE01
1CB4DD7EE01
1C44D67EE01 1C44D67EE01

regex to check 9 length characters for the prefix

I'm trying to create a regex to
valid following type string only
first 2 characters 0 to 9
next 3 characters 000 to 366 then 501 to 866
next 4 characters 0 to 9
finally x or X
so I did that using following
([0-9]{2}(001|002|003|004|005|006|007... |366|501|...|866)[0-9]{4}[x|X]$)
so this can validate properly 900033618x
but this also taking 9001033618x as valid onces
how to restrict to 9 character length before x or X
I tried do this, like following
(^\d[[0-9]{2}(001|002|003|004|005|006|007... |366|501|...|866)[0-9]{4}]{9}[x|X]$)
but this not working
Just do what you say:
^[0-9]{2}(?:[01][0-9][0-9]|[24]00|3(?:6[6-9]|[7-9][0-9]))[0-9]{4}[xX]$
Note: watch beginning caret ^

Regular expression for matching numbers and ranges of numbers

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.