regex - Using if and else in regular expression -
i'm having difficulty trying understand particular regular expression (it used check user input phone number) :
^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{1,4})(-| )?(\d{6})(( x| ext)\d{1,5}){0,1}$ i read "?()" used if condition in regular expression, still not clear me logic behind regular expression , kind of input accepted , rejected it.
thanks
firstly, in regexp, ?() not conditional. ? matches character (group) left of 0 or 1 times , () starts capture group nothing in it?... no conditionals i'm afraid :) closest might (a|b) matches either a or b...
the regexp little difficult read, so
^((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))(-| )?(\d{1,4})(-| )?(\d{6})(( x| ext)\d{1,5}){0,1}$ try regexper.com, type in regexp , draw state diagram...
using tabbing break expression:
^( (\+\d{1,3}(-| )? \(?\d\)?(-| )? \d{1,3}) |( \(?\d{2,3}\)? ) ) (-| )?(\d{1,4}) (-| )?(\d{6}) ( ( x| ext)\d{1,5} ){0,1}$ (note makes spaces hard read we'll go through referencing original)
^ matches start of line
the next group ((\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3})|(\(?\d{2,3}\)?))
this has 2 parts: (x|y), x=(\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3}) , y=(\(?\d{2,3}\)?). match either x or y...
breaking down x=(\+\d{1,3}(-| )?\(?\d\)?(-| )?\d{1,3}):
the outer
()capture, strip these...\+matches literal plus sign. note has escaped\because + meta character meaning "match 1 or more of previous".\d{1,3}matches decimal digit eiter 1, 2 or 3 times no more or less(-| )?matches either-or(space) 0 or 1 times.?wht specifies 0 or 1 times.\(?\d\)matches literal '(' (notice escape) 0 or 1 times. decimal digit, literal)(-| )?we've seen before (matches either-or(space) 0 or 1 times.?wht specifies 0 or 1 times.)\d{1,3}we've seen before (matches decimal digit eiter 1, 2 or 3 times no more or less)
so can x matches (and captures - that's wat outer () doing) string starts plus, has 1 3 digits possibly space or hyphen, digit inside brackets, possibly space or hyphen , 1 3 digits. captured first capture group... phew!
breaking down y=(\(?\d{2,3}\)?):
the outer
()capture string these...\(?matches literal(0 or 1 times.\d{2,3}matches digit 2 or 3 times\)?matches literal)0 or 1 times
so can y matches 2 or 3 digit number, possibly surrounded brackets. captures first capture group. jeez!
now have x , y can see first chunk of regexp matches (brain melting!).
the first chunk, call chunk1 matches , captures either
any string starts plus, has 1 3 digits possibly space or hyphen, digit inside brackets, possibly space or hyphen , 1 3 digits or
any 2 or 3 digit number, possibly surrounded brackets
continuing... (-| )? we've seen before (matches either - or (space) 0 or 1 times. ? wht specifies 0 or 1 times.)
(\d{1,4}) matches string of digit characters 1,2,3 or 4 digits in length. forms second capture group.
(-| )? we've seen before (matches either - or (space) 0 or 1 times. ? wht specifies 0 or 1 times.)
(\d{6}) matches string of 6 digits
so here matching string possible space or hypen, 1 4 numbers, possible space or hyphen , 6 numbers. call chunk2
so far have matched string consistiing of chunk1 followed chunk2...
this concludes main bit of phone number, rest appears handle extensions...
the next bit (( x| ext)\d{1,5}){0,1}. lets break down little.
the surrounding brackets capture group.
( x| ext)matches either of 2 literal strings ' x' or ' ext' - note beginning space.\d{1,5}matches digit 1,2,3,4 or 5 times.{0,1}matches capture group 0 or 1 times... i.e. phone number not need have extension
finally $ matches end of line.
hopefully has broken down string enough work through :)
Comments
Post a Comment