Page 1 of 1

trying to understand this regex

Posted: Wed Oct 11, 2006 1:21 pm
by iffo
Hi I have a question, this regax is being used to validate phone number what are they trying to do here?



$phone_regex = "/(\([2-9]\d{2}\)\s?|[2-9]\d{2}-|[2-9]\d{2})[1-9]\d{2}-?\d{4}/";

Posted: Wed Oct 11, 2006 2:30 pm
by nickvd

Code: Select all

/
   (  begin grouping
      \( look for a literal bracket
      [2-9] followed by a single number between 2 and 9
      \d{2} followed by two digits (0-9)
      \) followed by a literal bracket
      \s? there may or may not be a space after the bracket
   |                  OR
      [2-9]  a single digit (2-9)
      \d{2} followed by two digits
      -  followed by a hyphen
   |                  OR
      [2-9]  a single digit (2-9)
      \d{2} followed by two digits
   )  end grouping
   
   [1-9] followed by a digit from 1-9
   \d{2} followed by two more digits from 0-9
   -? followed by a hyphen (that may or not be there)
   \d{4} followed by 4 digits (0-9)
/
I whipped that up quickly, so i may have made an error... Upon simple examination it seems that it will catch a few phone numbers (us/canada only), but would fail on 1 (954) 334 3433, (965) 455 4544 (looking for the existance or absence of a hyphen)

Posted: Wed Oct 11, 2006 3:16 pm
by RobertGonzalez
Or a dot/chars (as some prefer: 800.eat.beef).