Regular Expression Validation For Indian Phone Number and Mobile number

前端 未结 12 755
后悔当初
后悔当初 2020-12-01 09:26

I want to validate Indian phone numbers as well as mobile numbers. The format of the phone number and mobile number is as follows:

For land Line number



        
12条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-01 10:16

    I use the following for one of my python project

    Regex

    (\+91)?(-)?\s*?(91)?\s*?(\d{3})-?\s*?(\d{3})-?\s*?(\d{4})
    

    Python usage

    re.search(re.compile(r'(\+91)?(-)?\s*?(91)?\s*?(\d{3})-?\s*?(\d{3})-?\s*?(\d{4})'), text_to_search).group()
    

    Explanation

    (\+91)? // optionally match '+91'
    (91)?   // optionally match '91'
    -?      // optionally match '-'
    \s*?    // optionally match whitespace
    (\d{3}) // compulsory match 3 digits
    (\d{4}) // compulsory match 4 digits
    

    Tested & works for

    9992223333
    +91 9992223333
    91 9992223333
    91999 222 3333
    +91999 222 3333
    +91 999-222-3333
    +91 999 222 3333
    91 999 222 3333
    999 222 3333
    +919992223333
    

提交回复
热议问题