What is the regex pattern to match a Zambian NRC number - regex

Zambian National Registration Numbers(NRC) follow a pattern of 6 digits followed by a forward slash, followed by 2 digits, then another forward slash and then 1 digit at the end. An example of an NRC number would be 111111/11/1.
What regular expression can I use to match this format of numbers and slashes.

This format should work ^\d{6}\/\d{2}\/\d{1}$
^ - means match at the start of the string
\d{6} - means match exactly 6 digits
\/ - means match the forward slash.
\d{2} - means match exactly 2 digits
\d{1} - means match exactly 1 digit
$ - means match at the end of the string
You can test the regex expression on this site: https://regex101.com/

and this also, and is the more portable solution:
^[0-9][0-9][0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9]$

/^\(?([0-9]{6})\)?\/([0-9]{2})\/([0-9]{1})$/
This should work too

The format of zambian NRC is [ , 6 digits between 0 and 9 ] / [, 2 digits between 0 and 9] / [, 1 which can either be 1 for zambian or 2 for a non zambian ]
therefore any derivative of the following should work:
^[0-9][0-9][0-9][0-9][0-9][0-9]/[0-9][0-9]/[1-2]

Related

Regex expression for numbers and leading zeros just with a dot and decimal

I'm trying to find a regex for numeric inputs. We can receive a leading 0 just if we add a dot for adding 1 or 2 decimal numbers. And of course just accept numbers.
These are the scenarios that we can accept:
0.01
1.1
1.02
120.01
We can't accept these values
0023
0100
.01
.12
Which regex is the best option for these cases?
Until now we try we the following regex for accepting just number and dots
[A-Za-z,]
And also we try with the following ones:
^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{1})?|(?:,[0-9]{3})*(?:\.[0-9]{1,2})?|(?:\.[0-9]{3})*(?:,[0-9]{1,2})?)$
"/^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/"
"/(^(\d{1})\.{0,1}([0-9]){0,2}$)|(^([1-9])\d{0,2}(\,\d{0,3})$)/g"
(?:0|[1-9][0-9]*)(?:\.[0-9]{1,2})?
And the next one for deleting the leading zeros but it didn't work for 0.10 cases
^0+
If a negative lookahead is supported, you can exclude matches that start with a zero and have no decimal part.
^(?!0\d*$)\d+(?:\.\d{1,2})?$
^ Start of string
(?!0+\d*$) Negative lookahead, assert not a zero followed by optional digits at the right
\d+ Match 1+ digits
(?:\.\d{1,2})? Match an optional decimal part with 1 or 2 digits
$ End of string
Regex demo
I would go with ^(0|[1-9]\d*|(0|[1-9]\d*)\.\d+)$
You can test here: https://regex101.com/r/oNMgR9/1
Explanation
^ means : match the beginning of the string (or line if the m flag is enabled).
$ means : match the end of the string (or line if the m flag is enabled).
(a|b) means match "a" or match "b" so I'll use this to match either "0" alone or any number not starting with a "0". It's the syntax for a logical or.
. alone is used to match any char. So you have to escape it if you want to match the dot character. This is why I wrote 0\. instead of 0..
[ ] is used to list some characters you want to match. It can be a range if you use the - char, so [1-9] means any digit char from "1" to "9".
\d is to match a digit. It's totally equivalent to [0-9].
* means : match the preceding pattern 0 or many times, so \d* means that it will match 0 or many times a digit, so it will match "8" or "465" or "09" but also an empty string "". If you want to match the preceding pattern at least once or many times then you use + instead of *. So \d+ won't match an empty string "" but \d* would match it.
A) Just a number not starting with 0
[1-9]\d* will match any digit from 1 to 9 and then optionnaly followed by other digits. This will match numbers without a decimal point.
B) Just 0
0 alone is a possibility. This is because the case above isn't covering it.
B) A number with decimals
(0|[1-9]\d*)\.\d+ will match either a "0" alone or a number not starting by "0" and then followed by a point and some other digits (which have to be present because we don't want to match "45." without the numbers behind the dot).
Better alternative
The solution from #TheFourthBird is a bit cleaner with the use of a negative lookahead. It's just a bit different to understand. And he read the question completely: You wanted 1 or 2 digits after the decimal. I forgot about that, so, effectively, \d+ should be replaced by \d{1,2} as you don't want more than 2 digits.
You can use
^(?![0.]+$)(?:[1-9]\d*|0)(?:\.\d{1,2})?$
See the regex demo.
Details:
^ - start of string
(?![0.]+$) - fail the match if there are just zeros or dots till end of string
(?:[1-9]\d*|0) - either a non-zero digit followed with any zero or more digits or a zero
(?:\.\d{1,2})? - optionally followed with a sequence of a . and one or two digits
$ - end of string.

Regex to not allow duplicate wild cards

I want to make regex which can pass the following cases:
02:12
10:23
00.23
0.23
.02
:88
Here is what i have tried: ^([0-9:. ])*[.: ]+$
But it allows duplicate" :, ., (space)", and also I'm not able to limit to 1-2 digits on both sides of wildcards. Any help would be great. Thanks
The pattern you tried only matches digits on the left side and matching the duplicates is due to the quantifiers.
If you want to allow 1 or 2 digits on both sides and make the digits on the left optional:
^[0-9]{0,2}[.:][0-9]{1,2}$
^ Start of string
[0-9]{0,2} Match 0, 1 or 2 times a digit 0-9
[.:] Match either . or :
[0-9]{1,2} Match 1 or 2 times a digit 0-9
$ End of string
Regex demo

Matching numbers with an optional delimiter in between

So i got this regex code /[0-3][0-9][0-1][1-9]\d{2}[-\s]\d{4}?[^0-9]*|[0-3][0-9][0-1][1-9]\d{2}\d{4}/
This regex code take this kind of numbers:
1002821187
100282 1187
100282-1187
But i found out i dont want the numbers: 1002821187
So is it possible to make 1 regex code that only finds:
100282 1187
100282-1187
Your regex contains an alternation that matches the numbers with and without the space or -. You need to require that space or hyphen:
^[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}$
^^^^^
See the regex demo. If you do not need to check for any boundaries, remove ^ and $ anchors that make the pattern match the whole string and use [0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}. Or use word boundaries to find whole words, \b[0-3][0-9][0-1][1-9][0-9]{2}[-\s][0-9]{4}\b.
Details
^ - start of string
[0-3] - a digit from 0 to 3
[0-9] - any digit
[0-1] (=[01]) - 0 or 1
[1-9] - any digit other than 0
[0-9]{2} - 2 digits
[-\s] - a - or whitespace
[0-9]{4} - 4 digits
$ - end of string.
Though you did not specify exactly what you are trying to do, here you can try the following regex :
\b\d{6}-\d{4}\b|\b\d{4}\b|\b\d{6}\b
Hope it helps.

Regex limiting a number string

I am trying to figure out how to use regex to pass a 6 digit number string. My trouble is the string can be any 6 digits, unless it starts with 12. So the first digit can be 1 but not if second digit is 2. The second digit can be 2, but not if the first is 1.
I tried this, ([^1])([^2])(\d{4}) but that does not take into account both digits, so it will block anything with a 2 in the second spot.
Thank you for any help.
You may use
^([02-9][0-9]|[0-9][013-9])[0-9]{4}$
See the regex demo
Details:
^ - start of string
([02-9][0-9]|[0-9][013-9]) - either of the two alternatives:
[02-9][0-9] - any digit but 1 and then any digit
| - or
[0-9][013-9] - any digit and then any digit but 2
[0-9]{4} - any 4 digits
$ - end of string.
Another way is to use a negative lookahead:
^(?!12)[0-9]{6}$
See another demo. Here, (?!12) fails the match if the first 2 digits are 12. The [0-9]{6} will match 6 digits.
Depending on the regex library/method, ^/$ anchors may not be required. Lookaheads are not always supported, too.

How to match a whole string that contains just two and no more than two digits between 0 and 10 in regex?

This regex does not work for me as selects all groups of two and multiple digits and not the string.
abcde9 = match
abcde12 = not matched
abcde12345678 = not matched
What I have at the moment is this, it I just can't include the 0 and the 10 as two digits numbers in the regex, can anyone help me?
\d{0,10}[1-9]
If you want to match any string containing exactly one integer from 0 to 10 then use
^\D*(\d|10)\D*$
which means "any non-digit content followed by either a single digit or the number 10 and then followed by any non-digit content"
try it at regex101
I think you are looking for
^\D*(?:[0-9]|10)(?:\D+(?:[0-9]|10))?\D*$
See demo
This will match a whole string that contains 1 or 2 whole integer numbers from 0 to 10, and no other digits.
The regex breakdown:
^ - start of string
\D* - 0 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
(?:\D+(?:[0-9]|10))? - 1 or 0 occurrence of
\D+ - 1 or more characters other than digit
(?:[0-9]|10) - numbers from 0 to 10
\D* - 0 or more characters other than digit
$ - end of string
Is that what you looking for:
/(0[1-9])$/
You can test that regex to make sure it fits your needs:
https://regex101.com/r/hX6lB7/3