On 11/25/2010 04:10 PM, Kenneth Gonsalves wrote:
On Thu, 2010-11-25 at 15:56 +0530, Anand Balachandran Pillai wrote:
 >  r'(^0\d{2}[-\s]{1}[1-6]{1}\d{7})|(^0\d{3}[-\s]{1}[1-6]{1}\d{6})|(^0
 >  \d{4}[-\s]{1}[1-6]{1}\d{5})'
 >

 It is doable, but you should really use pyparsing for this - this is
 UGLY !

I know - but everything I tried to make it look good did not work. And
the app in which I am plugging this into requires an re.

Certainly, you can beautify that using the verbose option. Here is my attempt. (Note, I use the beautiful (?(id/name)yes-pattern|no-pattern) syntax for the black magic :)

phone_re = re.compile(r"""
    (^0                             # all std-codes start with 0
        (
            (?P<twodigit>\d{2})   | # the std-code group
            (?P<threedigit>\d{3}) | # either two, three or four digits
            (?P<fourdigit>\d{4})    # following the 0
        )
        [-\s]                       # space or -
        [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)


hth,
cheers,
- steve
_______________________________________________
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers

Reply via email to