Home Backend Development Golang How to verify mobile phone number operator using regular expression in golang

How to verify mobile phone number operator using regular expression in golang

Jun 24, 2023 am 08:17 AM
regular expression phone number Operator

When developing a program, it is often necessary to verify whether the entered mobile phone number is legal and which operator the mobile phone number belongs to. This process can be easily achieved using regular expressions in golang. This article will introduce how to use regular expressions to verify mobile phone number operators in golang.

1. Verify whether the mobile phone number is legal

In golang, you can use regular expressions to verify whether the mobile phone number is legal. The following is a sample code:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    // 手机号码正则表达式
    mobileExp := `^(1[3-9]d{9})$`

    // 编译正则表达式
    mobileReg := regexp.MustCompile(mobileExp)

    // 测试手机号码
    mobile := "13212345678"
    if mobileReg.MatchString(mobile) {
        fmt.Printf("手机号码 %s 合法
", mobile)
    } else {
        fmt.Printf("手机号码 %s 不合法
", mobile)
    }
}
Copy after login

In the above code, a regular expression for a mobile phone number is first defined, and then the regexp.MustCompile() method is used to compile it into a regular expression object. Finally, use the MatchString() method to verify whether the mobile phone number is legal. If the verification is passed, "Mobile phone number xxx is legal" is output, otherwise "Mobile phone number xxx is illegal" is output.

In the above code, the regular expression of the mobile phone number is ^(1[3-9]d{9})$, which represents an 11-digit number starting with 1. Among them, the second digit is a number between 3 and 9, and the following 10 digits can be any number. The regular expression can be modified as needed to validate mobile phone numbers in a specific format.

2. Verify the operator of the mobile phone number

In golang, you can use the mobile phone number segments of China Telecom, China Unicom and China Mobile to determine the operator of the mobile phone number.

China Telecom’s mobile phone number ranges are: 133, 149, 153, 173, 177, 180, 181, 189, 199;

China Unicom’s mobile phone number ranges are: 130, 131, 132, 145, 155, 156, 166, 175, 176, 185, 186;

China Mobile’s mobile phone number segments are: 134, 135, 136, 137, 138, 139, 147, 148, 150, 151, 152, 157, 158, 159, 165, 172, 178, 182, 183, 184, 187, 188, 198.

The following is a sample code that can output corresponding information according to the operator corresponding to the mobile phone number:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    // 手机号码正则表达式
    mobileExp := `^(1[3-9]d{9})$`

    // 编译正则表达式
    mobileReg := regexp.MustCompile(mobileExp)

    // 测试手机号码
    mobile := "13412345678"
    if mobileReg.MatchString(mobile) {
        if mobile[0:3] == "133" || mobile[0:3] == "149" || mobile[0:3] == "153" || mobile[0:3] == "173" || mobile[0:3] == "177" || mobile[0:3] == "180" || mobile[0:3] == "181" || mobile[0:3] == "189" || mobile[0:3] == "199" {
            fmt.Printf("手机号码 %s 是中国电信", mobile)
        } else if mobile[0:3] == "130" || mobile[0:3] == "131" || mobile[0:3] == "132" || mobile[0:3] == "145" || mobile[0:3] == "155" || mobile[0:3] == "156" || mobile[0:3] == "166" || mobile[0:3] == "175" || mobile[0:3] == "176" || mobile[0:3] == "185" || mobile[0:3] == "186" {
            fmt.Printf("手机号码 %s 是中国联通", mobile)
        } else if mobile[0:3] == "134" || mobile[0:3] == "135" || mobile[0:3] == "136" || mobile[0:3] == "137" || mobile[0:3] == "138" || mobile[0:3] == "139" || mobile[0:3] == "147" || mobile[0:3] == "148" || mobile[0:3] == "150" || mobile[0:3] == "151" || mobile[0:3] == "152" || mobile[0:3] == "157" || mobile[0:3] == "158" || mobile[0:3] == "159" || mobile[0:3] == "165" || mobile[0:3] == "172" || mobile[0:3] == "178" || mobile[0:3] == "182" || mobile[0:3] == "183" || mobile[0:3] == "184" || mobile[0:3] == "187" || mobile[0:3] == "188" || mobile[0:3] == "198" {
            fmt.Printf("手机号码 %s 是中国移动", mobile)
        } else {
            fmt.Printf("手机号码 %s 不是中国电信、中国联通或中国移动", mobile)
        }
    } else {
        fmt.Printf("手机号码 %s 不合法
", mobile)
    }
}
Copy after login

In the above code, first verify whether the mobile phone number is legal. If the mobile phone number is legal, the corresponding information will be output based on the first 3 digits corresponding to the mobile phone number.

To sum up, by using regular expressions in golang, mobile phone number verification and operator judgment can be easily achieved. In actual development, the code can be optimized as needed, such as saving regular expressions and the mobile phone number segments of each operator as variables to achieve more flexible verification and judgment.

The above is the detailed content of How to verify mobile phone number operator using regular expression in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check what is registered with a mobile phone number 'Detailed explanation: APP query method for mobile phone number registration' How to check what is registered with a mobile phone number 'Detailed explanation: APP query method for mobile phone number registration' Feb 07, 2024 am 08:24 AM

I don’t know if you have such an experience. Your mobile phone often receives some inexplicable text messages, or registration information for some websites or other verification information. In fact, our mobile phone number may be bound to many unfamiliar websites, and we ourselves Even if you don’t know, what I will share with you today is to teach you how to unbind all unfamiliar websites with one click. Step 1: Open the number service platform. This technique is very practical. The steps are as follows: Open WeChat, click the plus icon in the search box, select Add Friend, and then enter the code number service platform to search. We can see that there is a number service platform. Of course, it belongs to a public institution and was launched by the National Institute of Information and Communications Technology. It can help everyone unbind mobile phone number information with one click. Step 2: Check whether the phone has been marked for me

How to change the mobile phone number of Amap - Detailed introduction to the method of changing the mobile number of Amap How to change the mobile phone number of Amap - Detailed introduction to the method of changing the mobile number of Amap Mar 20, 2024 pm 08:41 PM

Amap is loved by users for its precise positioning and rich functions. However, during use, sometimes we may need to change the bound mobile phone number to ensure the accuracy of personal information and services. So, how to change the mobile phone number of Amap? The editor has compiled some relevant information, come and take a look with me! How to change the mobile phone number on Amap? Answer: [Amap]-[My]-[Settings Icon]-[Account and Security]-[Mobile Number]-[Change]-[Next Step]. Specific steps: 1. First open the Amap software and enter the home page. We need to click [My] in the lower right corner; 2. Then you can see some related functions in My One Second. Here we click on the upper right corner. [Settings icon] in the corner;

How to check the usage status of mobile phone number? How to check the usage time of mobile phone number? How to check the usage status of mobile phone number? How to check the usage time of mobile phone number? Mar 07, 2024 pm 04:30 PM

With the continuous advancement of mobile communication technology, mobile phone numbers have become an indispensable communication tool in our daily lives. Sometimes, we need to check the usage status of a mobile phone number, such as verifying whether the number is out of service or in arrears. 1. How to check the usage status of mobile phone number? Contacting the operator's customer service is the most direct way to check the usage status of your mobile phone number. Different operators may have different inquiry methods, but this can usually be accomplished by calling the operator's customer service hotline. When talking to customer service staff, you only need to provide the mobile phone number you need to query, and the customer service staff will provide you with detailed usage status information. Therefore, if you want to know the specific situation of a certain mobile phone number, you can get relevant information by calling the operator's customer service hotline. There are

How to change the mobile phone number binding in QQ mailbox - How to change the mobile phone number binding in QQ mailbox How to change the mobile phone number binding in QQ mailbox - How to change the mobile phone number binding in QQ mailbox Mar 04, 2024 pm 03:46 PM

Many friends don’t know how to change the mobile phone number binding in QQ mailbox, so the editor below will share the method of changing the mobile phone number binding in QQ mailbox. Let’s follow the editor to take a look. I believe it will be helpful to everyone. Step 1: First open the QQ mailbox and select the settings above, as shown below. Step 2: Select the account option in the settings, as shown below. Step 3: Choose to log out immediately, as shown below. Step 4: After logging out, choose to register a mobile phone number and email account. Step 5: Choose to register other accounts in the interface that appears, as shown below. Step 6: Use the new mobile phone number according to the prompts and send text messages, as shown below. Step 7: After the sending is completed, the change is successful. The above is the entire content of how to change the mobile phone number binding in QQ mailbox brought to you by the editor. I hope

PHP regular expression validation: number format detection PHP regular expression validation: number format detection Mar 21, 2024 am 09:45 AM

PHP regular expression verification: Number format detection When writing PHP programs, it is often necessary to verify the data entered by the user. One of the common verifications is to check whether the data conforms to the specified number format. In PHP, you can use regular expressions to achieve this kind of validation. This article will introduce how to use PHP regular expressions to verify number formats and provide specific code examples. First, let’s look at common number format validation requirements: Integers: only contain numbers 0-9, can start with a plus or minus sign, and do not contain decimal points. floating point

How to validate email address in Golang using regular expression? How to validate email address in Golang using regular expression? May 31, 2024 pm 01:04 PM

To validate email addresses in Golang using regular expressions, follow these steps: Use regexp.MustCompile to create a regular expression pattern that matches valid email address formats. Use the MatchString function to check whether a string matches a pattern. This pattern covers most valid email address formats, including: Local usernames can contain letters, numbers, and special characters: !.#$%&'*+/=?^_{|}~-`Domain names must contain at least One letter, followed by letters, numbers, or hyphens. The top-level domain (TLD) cannot be longer than 63 characters.

How to match timestamps using regular expressions in Go? How to match timestamps using regular expressions in Go? Jun 02, 2024 am 09:00 AM

In Go, you can use regular expressions to match timestamps: compile a regular expression string, such as the one used to match ISO8601 timestamps: ^\d{4}-\d{2}-\d{2}T \d{2}:\d{2}:\d{2}(\.\d+)?(Z|[+-][0-9]{2}:[0-9]{2})$ . Use the regexp.MatchString function to check if a string matches a regular expression.

PHP regular expressions: exact matching and exclusion of fuzzy inclusions PHP regular expressions: exact matching and exclusion of fuzzy inclusions Feb 28, 2024 pm 01:03 PM

PHP Regular Expressions: Exact Matching and Exclusion Fuzzy inclusion regular expressions are a powerful text matching tool that can help programmers perform efficient search, replacement and filtering when processing text. In PHP, regular expressions are also widely used in string processing and data matching. This article will focus on how to perform exact matching and exclude fuzzy inclusion operations in PHP, and will illustrate it with specific code examples. Exact match Exact match means matching only strings that meet the exact condition, not any variations or extra words.

See all articles