Home  >  Article  >  Backend Development  >  How to use regular expressions to verify the legitimacy of ISBN numbers in golang

How to use regular expressions to verify the legitimacy of ISBN numbers in golang

PHPz
PHPzOriginal
2023-06-24 11:50:341009browse

ISBN (International Standard Book Number) is a numerical code used to identify books. It consists of 13 digits, usually starting with "978" or "979". In golang, you can use regular expressions to verify the legitimacy of the ISBN number. This article will introduce how to use regular expressions to verify ISBN numbers.

First of all, using regular expressions in golang requires introducing the regexp package. The regexp package provides a regular expression engine that can be used to match and search strings. Next, define a regular expression for the ISBN number:

^[0-9]{13}$

This regular expression represents a string of 13 digits. ^ represents the beginning of the string, and $ represents the end of the string. [0-9] represents a number, {13} represents that the number appears 13 times in a row.

The following is a sample code that demonstrates how to use regular expressions to verify the legitimacy of an ISBN number:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    // 定义ISBN号码的正则表达式
    isbnRegex := regexp.MustCompile(`^[0-9]{13}$`)

    // 测试数据
    testCases := []struct {
        input    string
        expected bool
    }{
        {"9780134190440", true},
        {"978-013-419-044-0", false},
        {"1234567890123", true},
        {"1234567890123456", false},
    }

    // 循环测试数据,进行验证
    for _, testCase := range testCases {
        actual := isbnRegex.MatchString(testCase.input)
        fmt.Printf("input: %s, expected: %t, actual: %t
", testCase.input, testCase.expected, actual)
    }
}

In this example, we first define a regular expression for an ISBN number, and Compile it using the Compile method. Next, a set of test data is defined, including ISBN numbers and desired verification results. Finally, use the MatchString method to verify the legitimacy of the ISBN number and output the verification result.

Run the code and you will get the following output:

input: 9780134190440, expected: true, actual: true
input: 978-013-419-044-0, expected: false, actual: false
input: 1234567890123, expected: true, actual: true
input: 1234567890123456, expected: false, actual: false

As you can see from the output, it is very simple to use regular expressions to verify the legitimacy of the ISBN number.

In summary, using regular expressions to verify the legitimacy of ISBN numbers in golang only requires defining a regular expression that conforms to the format and using the MatchString method. Through this method, the ISBN number can be easily verified.

The above is the detailed content of How to use regular expressions to verify the legitimacy of ISBN numbers in golang. 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