I am trying to do regex validation for 11 digit mobile number of type 03025398448.Where first 3 digits are constant 030 and remaining 8 digits are from 0 to 9 (any number) and 1st digit could be written in +92 format .So, help me for this number regex code
If the number should start with 030 and +92 is optional and when using +92 you should omit the leading zero, you could use:
^(?:\+9230|030)?\d{8}$
Explanation
^ # From the beginning of the string
(?: # Non capturing group
\+9230|030 # Match +9230 or 030
)? # close capturing group and make it optional
\d{8} # Match 8 digits
$ # The end of the string
In C# you could use this as string pattern = #"^(?:\+9230|030)?\d{8}$";
C# code
You can use this regular expression:
^((\+?92)30[0-9]{8}|030[0-9]{8})$
Explanation
BeginOfLine
CapturingGroup
GroupNumber:1
OR: match either of the followings
Sequence: match all of the followings in order
CapturingGroup
GroupNumber:2
Sequence: match all of the followings in order
Repeat
+
optional
9 2
3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
Sequence: match all of the followings in order
0 3 0
Repeat
AnyCharIn[ 0 to 9]
8 times
EndOfLine
Related
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]
How I can write a regex which accepts 10 or 14 digits separated by a single space in groups of 1,2 or 3 digits?
examples:
123 45 6 789 1 is valid
1234 567 8 9 1 is not valid (group of 4 digits)
123 45 6 789 109 123 8374 is not valid (not 10 or 14 digits)
EDIT
This is what I have tried so far
[0-9 ]{10,14}+
But it validates also 11,12,13 numbers, and doesn't check for group of numbers
You may use this regex with lookahead assertion:
^(?=(?:\d ?){10}(?:(?:\d ?){4})?$)\d{1,3}(?: \d{1,3})+$
RegEx Demo
Here (?=...) is lookahead assertion that enforces presence of 10 or 14 digits in input.
\d{1,3}(?: \d{1,3})+ matches input with 1 to 3 digits separated by space with no space allowed at start or end.
aggtr,
You can match your use case with the following:
^(?:\d\s?){10}$|^(?:\d\s?){14}$
^ means the beginning of the string and $ means the end of the string.
(?:...) means a non-capturing group. Thus, the part before the | means a string that starts and has a non-capturing group of a decimal followed by an optional space that has exactly 10 items followed by the end of the string. By putting the | you allow for either 10 or 14 of your pattern.
Edit I missed the part of your requirement to have the digits grouped by 1, 2, or 3 digits.
I am looking for a specific sequence of numbers in a line. I can best explain with an example:
00001 # first search criteria - line 1
00010 # second search criteria - line 2
So every line has 5 digits of either 0 or 1. I am looking for the combination of all 0 except for 1 digit that can be a 1. This 1 can be in any position of the 5 digits.
The regex code I have for 5 digits of 0 is
^((0\s*?){5}) # there may be spaces between the numbers
The line 1 case above would be selected with following regex code:
^((0\s*?){4})\s*(1)
My question is how I could write in regex code the changing position of 1 to cover the 5 cases/positions.
Thank you.
You can use a lookahead based regex for this:
^(?=[0\s]*1[0\s]*$)(?:\s*[01]\s*){5}$
RegEx Demo
Lookahead (?=[0\s]*1[0\s]*$) will enforce only single 1 at any position in input where as (?:\s*[01]\s*){5} will make sure that input has only 0 and 1 with 5 digits length also allowing white-spaces anywhere.
You can use two conditionals.
First one insures 1 is not found again.
Second one insures 1 is found.
(?:((?(1)(?!))1)|0){5}(?(1)|(?!))
Expanded
(?:
( # (1 start)
(?(1) (?!) ) 1
) # (1 end)
| 0
){5}
(?(1) | (?!) )
I have problem with one regular expression to check if a (french) phone number is correct.
Phone number must start with one 0 continue with one 1 or 2 or 3 or 4 or 5 or 9 and continue with 8 numbers but theses numbers must not be the same like 00000000 or 11111111...
My current regular expression :
/^0(1|2|3|4|5|9){1,1}[0-9]{8,8}/i
Thanks in advance for help.
You can use the following regex:
/^0[1-59](?!(\d)\1{7}$)\d{8}$/i
Some points:
{1,1} is as good as being removed.
{8,8} is as good as {8}.
(1|2|3|4..) can be replaced with character class - [1234]
[12345] can be replaced by range in a character class - [1-5]
The above regex uses negative look-ahead assertion - (?!(\d)\1{7}) to assert that the 8 digits after first 2, are not all same. If the assertion is true, then it matches the next 8 digits.
(\d) captures the first digit in group 1
Then \1 backreferences the captured group to match the same digit that was matched by \d
{7} matches the backreference 7 times. That means - (\d)\1{7} matches same digit 8 times.
I'm trying:
\d{3}|\d{11}|\d{11}-\d{1}
to match three-digit numbers, eleven-digit numbers, eleven-digit followed by a hyphen, followed by one digit.
But, it only matches three digit numbers!
I also tried \d{3}|\d{11}|\d{11}-\d{1} but doesn't work.
Any ideas?
There are many ways of punctuating phone numbers. Why don't you remove everything but the digits and check the length?
Note that there are several ways of indicating "extension":
+1 212 555 1212 ext.35
If the first part of an alternation matches, then the regex engine doesn't even try the second part.
Presuming you want to match only three-digit, 11 digit, or 11 digit hyphen 1 digit numbers, then you can use lookarounds to ensure that the preceding and following characters aren't digits.
(?<!\d)(\d{3}|\d{11}|\d{11}-\d{1})(?!\d)
\d{7}+\d{4} will select an eleven digit number. I could not get \d{11} to actually work.
This should work: /(?:^|(?<=\D))(\d{3}|\d{11}|\d{11}-\d{1})(?:$|(?=\D))/
or combined /(?:^|(?<!\d))(\d{3}|\d{11}(?:-\d{1})?)(?:$|(?![\d-]))/
expanded:
/ (?:^ | (?<!\d)) # either start of string or not a digit before us
( # capture grp 1
\d{3} # a 3 digit number
| # or
\d{11} # a 11 digit number
(?:-\d{1})? # optional '-' pluss 1 digit number
) # end capture grp 1
(?:$ | (?![\d-])) # either end of string or not a digit nor '-' after us
/