Home  >  Article  >  Backend Development  >  How to match phone numbers using regular expression in Go language

How to match phone numbers using regular expression in Go language

PHPz
PHPzOriginal
2023-07-12 09:55:361761browse

How to use regular expressions to match phone numbers in Go language

Introduction:
In daily development, it is often necessary to format the phone number to ensure that the input phone number meets the specifications. The Go language provides powerful regular expression support, which can implement phone number matching and verification simply and efficiently. This article will introduce how to use regular expressions to match phone numbers in Go language, and provide code examples.

1. Basic use of regular expressions in Go language
Regular expression is a powerful pattern matching tool that can be used to quickly find text of a specific pattern in a string. In the Go language, regular expression related operations are encapsulated in the regexp package. To use regular expressions, you first need to import the regexp package:

import "regexp"

Then, you can compile a regular expression by calling the Compile function in the regexp package:

regexp.Compile(pattern string)

Among them, pattern is the regular expression string to be matched, and the Compile function will return a Regexp object for subsequent matching operations.

2. Regular expression for matching phone numbers
The format of phone numbers is different in various countries and regions. This article takes the phone number format of mainland China as an example. Phone numbers in mainland China are generally 11 digits starting with 1, such as: 13812345678. According to this rule, we can write the following regular expression to match phone numbers:

pattern := ^1[3456789]d{9}$

where, ^ indicates the beginning of the line, 1 indicates starting with 1, [3456789] indicates that the second digit is a number among 3, 4, 5, 6, 7, 8, and 9, d{9} indicates that the following 9 digits are any number, $ indicates the end of the line. In this way, the entire regular expression means that it will only match phone numbers that are 11 digits and start with 1.

3. Sample code for using regular expressions to match phone numbers in Go language
The following is a sample code that demonstrates how to use regular expressions to match phone numbers in Go language:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    phoneNumbers := []string{
        "13812345678",
        "18588888888",
        "13612345678",
        "13312345678",
        "12345678901",
    }

    pattern := `^1[3456789]d{9}$`
    r, _ := regexp.Compile(pattern)

    for _, phoneNumber := range phoneNumbers {
        if r.MatchString(phoneNumber) {
            fmt.Println(phoneNumber, "是有效的电话号码")
        } else {
            fmt.Println(phoneNumber, "不是有效的电话号码")
        }
    }
}

Run the above code, the output result is as follows:

13812345678 是有效的电话号码
18588888888 是有效的电话号码
13612345678 是有效的电话号码
13312345678 是有效的电话号码
12345678901 不是有效的电话号码

You can see from the running results that the first four phone numbers comply with the matching rules of the regular expression, but the last phone number does not comply with the rules.

Conclusion:
This article introduces how to use regular expressions to match phone numbers in Go language. By writing a suitable regular expression, you can quickly verify that the phone number entered by the user conforms to the specification. In actual development, remember to carefully write and test regular expressions to ensure that the desired pattern is accurately matched.

The above is the detailed content of How to match phone numbers using regular expression in Go language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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