Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- phone_re = re.compile(r"""
- (
- (?P<std_code> # the std-code group
- ^0 # all std-codes start with 0
- (
- (?P<twodigit>\d{2}) | # either two, three or four digits
- (?P<threedigit>\d{3}) | # following the 0
- (?P<fourdigit>\d{4})
- )
- )
- [-\s] # space or -
- (?P<phone_no> # the phone number group
- [1-6] # first digit of phone number
- (
- (?(twodigit)\d{7}) | # 7 more phone digits for 3 digit stdcode
- (?(threedigit)\d{6}) | # 6 more phone digits for 4 digit stdcode
- (?(fourdigit)\d{5}) # 5 more phone digits for 5 digit stdcode
- )
- )
- )$""", re.VERBOSE)
- import unittest
- class PhoneTests(unittest.TestCase):
- def test_std(self):
- self.assertTrue(re.search(phone_re, '012-11234567').group('std_code'), '012')
- self.assertTrue(re.search(phone_re, '0123-1123456').group('std_code'), '0123')
- self.assertTrue(re.search(phone_re, '01234-112345').group('std_code'), '01234')
- def test_phone_no(self):
- self.assertTrue(re.search(phone_re, '012-11234567').group('phone_no'), '11234567')
- self.assertTrue(re.search(phone_re, '0123-1123456').group('phone_no'), '1123456')
- self.assertTrue(re.search(phone_re, '01234-112345').group('phone_no'), '112345')
- def test_space_seperator(self):
- self.assertTrue(re.search(phone_re, '012 11234567').group('std_code'), '012')
- self.assertTrue(re.search(phone_re, '012 11234567').group('phone_no'), '11234567')
- if __name__ == '__main__':
- unittest.main()
Advertisement
Add Comment
Please, Sign In to add comment