Regex to validate phone number pattern - regex

How can I construct regx to validate phone number? That is:
First digit must be 04 or 050 , length range between 8-13
First digit cannot be 43 or 44 , first digit must be 4 or 9 and length should be 8 digits
I have tried this pattern:
^[04,050]\\d{8,13}
Can any body help me?

Let's break it down (hoping I understand correctly):
^ # Start of string
(?: # Match one of the following:
04\d{6,11} # Either an 8-13 digit number starting with 04
| # or
050\d{5,10} # an 8-13 digit number starting with 050
| # or
4[0-25-9]\d{6} # an 8 digit number starting with 4 but not 43 or 44
| # or
9\d{7} # an 8 digit number starting with 9
) # End of alternation
$ # End of string

Related

regex: Numbers and spaces (10 or 14 numbers)

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.

regex input validation for mobile number 03025498448 using C#

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

Regex - accept value between 5 and 555

I have a text box. This text box should accept only values between 5 and 555. But I am unable to achieve it.
I have tried the below:
/^[0-9]{5, 500}$/
Kindly help me in this case.
What you need:
^([5-9]|\d\d|[1-4]\d\d|5[0-4]\d|55[0-5])$
It uses | to have 3 different statements to deal with 1-digit, 2-digit and 3-digit numbers.
Live Demo on Regex101
How it works:
^ # String starts with ...
(
[5-9] # 1-Digit - Any digit 5 or over (5 to 9)
| # OR
\d\d # 2-Digit - Any 2 digits (since any 2-digit number will be within your range)
| # OR
[1-4]\d\d # 3-Digit (below 500) - Any digit 1 to 5 (100 to 500), followed by any 2 digits
| # OR
5[0-4]\d # 3-Digit (above 500, below 550) - 5, followed by any digit 0 to 4 (500 to 540),
# followed by any digit
| # OR
55[0-5] # 3-Digits (550 or above) - 55, followed by any digit 0 to 5 (550 to 555)
)
$ # ... String ends with
This should work (though not recommended)
^(55[0-5]|5[0-4][0-9]|[1-4][0-9][0-9]|[1-9][0-9]|[5-9])$
Regex Demo
Proof of correctness

Regular Exp to validate for Zero's

I have to validate for zero values in my text field, the length of text field is 4 chrs.
If the user enters 0 or 00 or 000 or 0000. this should fail.
presently i have written exp to validate values below 7500 which will also accept 0000.
please help me to tweek this to fail for 0 or 00 or 000 or 0000 values.
/^0*([0-9]{1,3}|[1-6][0-9]{3}|7[0-4][0-9]{2}|7500)$/
Thanks Punith
/^(?:[1-9]\d{0,2}|[1-6]\d{3}|7[0-4]\d{2}|7500)$/
Explanation:
^ # Start of string
(?: # Either match...
[1-9]\d{0,2} # 1-999
| # or
[1-6]\d{3} # 1000-6999
| # or
7[0-4]\d{2} # 7000-7499
| # or
7500 # 7500
) # End of alternation
$ # End of string
If you want to allow leading zeroes, then you can add 0* right after the ^. But then the length restriction to four digits is lost, of course.

Regular expression for phone numbers

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
/