Hi Everybody
Today we will learn some regular expression syntax while validating an international phone number for usa and uk.
Following are the format we are going to use an example:
+44 (1232582358)
+1 1472583695
+44 (123)(258)(2358)
+1 (123)(258)(2358)
+1 (123).(258).(2358)
+1 (123)-(258)-(2358)
+1 (123)-258-2358
+1 (0)(123)-258-2358
Seeing above format means, number always start with '+' sign and followed by 1 or 44.
So we will start our expression with \+(44|1)
After that can be space, hyphen(-) or dot (.) will come.
For that we will write ([ -\.])?
Here "?" means 0 or one of either space,hyphen or dot.
After that some people do write (0) for their national representation. E.g
+1 (0)(123)-258-2358
Some people write some not. So either it will come at one time or not come.
For that we will write (\(0\))?, means either (0) will come one time or not come.
After that , people write their 10 digit number with bracket or without. or 4-3-3 format or 3-3-4 format with or without bracket. Some people use dot(.) or space instead of hyphen.
Below expression cover all those :
(\(?[\d]{10}\)?|(\(?[\d]{4}\)?([ -\.])*\(?[\d]{3}\)?([ -\.])*\(?[\d]{3}\)?)|\(?[\d]{3}\)?([ -\.])*\(?[\d]{3}\)?([ -\.])*\(?[\d]{4}\)?)$
I am going to explain this in step by step.
(\(?[\d]{10}\)? means any digit of whose length is 10 with or without bracket.
"|" sign represents or.
([ -\.])? represents either space , hyphen or dot will come or not come.
\(?[\d]{3}\)? means any digit of whose length is 3 with or without bracket.
After that i have use same expression with "|" sign.
And at the end of expression $ sign represents end of expression.
Hope it will help you to understand some basic use of regular expression syntax.
Thanks
Today we will learn some regular expression syntax while validating an international phone number for usa and uk.
Following are the format we are going to use an example:
+44 (1232582358)
+1 1472583695
+44 (123)(258)(2358)
+1 (123)(258)(2358)
+1 (123).(258).(2358)
+1 (123)-(258)-(2358)
+1 (123)-258-2358
+1 (0)(123)-258-2358
So we will start our expression with \+(44|1)
After that can be space, hyphen(-) or dot (.) will come.
For that we will write ([ -\.])?
Here "?" means 0 or one of either space,hyphen or dot.
After that some people do write (0) for their national representation. E.g
+1 (0)(123)-258-2358
Some people write some not. So either it will come at one time or not come.
For that we will write (\(0\))?, means either (0) will come one time or not come.
After that , people write their 10 digit number with bracket or without. or 4-3-3 format or 3-3-4 format with or without bracket. Some people use dot(.) or space instead of hyphen.
Below expression cover all those :
(\(?[\d]{10}\)?|(\(?[\d]{4}\)?([ -\.])*\(?[\d]{3}\)?([ -\.])*\(?[\d]{3}\)?)|\(?[\d]{3}\)?([ -\.])*\(?[\d]{3}\)?([ -\.])*\(?[\d]{4}\)?)$
I am going to explain this in step by step.
(\(?[\d]{10}\)? means any digit of whose length is 10 with or without bracket.
"|" sign represents or.
([ -\.])? represents either space , hyphen or dot will come or not come.
\(?[\d]{3}\)? means any digit of whose length is 3 with or without bracket.
After that i have use same expression with "|" sign.
And at the end of expression $ sign represents end of expression.
Hope it will help you to understand some basic use of regular expression syntax.
Thanks
No comments:
Post a Comment