Guest User

Indian phone number re

a guest
Nov 25th, 2010
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.76 KB | None | 0 0
  1. import re
  2.  
  3. phone_re = re.compile(r"""
  4.     (
  5.         (?P<std_code>                   # the std-code group
  6.             ^0                          # all std-codes start with 0
  7.             (
  8.                 (?P<twodigit>\d{2})   | # either two, three or four digits
  9.                 (?P<threedigit>\d{3}) | # following the 0
  10.                 (?P<fourdigit>\d{4})
  11.             )
  12.         )
  13.         [-\s]                           # space or -
  14.         (?P<phone_no>                   # the phone number group
  15.             [1-6]                       # first digit of phone number
  16.             (
  17.                 (?(twodigit)\d{7})   |  # 7 more phone digits for 3 digit stdcode
  18.                 (?(threedigit)\d{6}) |  # 6 more phone digits for 4 digit stdcode
  19.                 (?(fourdigit)\d{5})     # 5 more phone digits for 5 digit stdcode
  20.             )
  21.         )
  22.     )$""", re.VERBOSE)
  23.  
  24. import unittest
  25.  
  26. class PhoneTests(unittest.TestCase):
  27.  
  28.     def test_std(self):
  29.         self.assertTrue(re.search(phone_re, '012-11234567').group('std_code'), '012')
  30.         self.assertTrue(re.search(phone_re, '0123-1123456').group('std_code'), '0123')
  31.         self.assertTrue(re.search(phone_re, '01234-112345').group('std_code'), '01234')
  32.  
  33.     def test_phone_no(self):
  34.         self.assertTrue(re.search(phone_re, '012-11234567').group('phone_no'), '11234567')
  35.         self.assertTrue(re.search(phone_re, '0123-1123456').group('phone_no'), '1123456')
  36.         self.assertTrue(re.search(phone_re, '01234-112345').group('phone_no'), '112345')
  37.  
  38.     def test_space_seperator(self):
  39.         self.assertTrue(re.search(phone_re, '012 11234567').group('std_code'), '012')
  40.         self.assertTrue(re.search(phone_re, '012 11234567').group('phone_no'), '11234567')
  41.  
  42.  
  43. if __name__ == '__main__':
  44.     unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment