Home > Backend Development > Golang > Use regular expressions in golang to verify whether the input is a legal identity document number

Use regular expressions in golang to verify whether the input is a legal identity document number

WBOY
Release: 2023-06-24 11:48:47
Original
1450 people have browsed it

For application scenarios that require verification of ID numbers, it is a common practice to use regular expressions to determine whether the input is legal. Golang provides the regexp package, which can be used to perform regular expression matching operations. This article will introduce how to use regular expressions in golang to verify whether the input ID number is legal.

1. ID card number rules

The ID number is composed of 18 digits and a check code. The specific format is as follows:

1~2 digits: indicates Provinces are represented by statistical codes designated by the National Bureau of Statistics;

3rd to 4th digits: represent cities, specified by provincial and autonomous region codes;

5th to 6th digits: represent districts and counties, Determined by city, county, and district codes;

7th to 14th digits: indicates the date of birth, for example:

19900907表示生于1990年9月7日
Copy after login

15th to 17th digits: indicates a sequence number, usually an odd number

The 18th digit: indicates the check code, generated according to the regulations of ISO 7064:1983.MOD 11-2.

2. Use regular expressions to verify ID numbers

In golang, you can use regular expressions to match whether the ID number conforms to the rules. The specific regular expression is as follows:

^([1-9]d{5})(19|20)d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|(3[0-1]))d{3}[0-9xX]$
Copy after login

Among them, ^ represents the starting position of the matching string, and $ represents the end position of the matching string. ([1-9]d{5}) means matching the first six digits of the ID number, that is, the province and city code. (19|20)d{2} represents the year of birth matching the ID number, where 19 and 20 represent the first two digits of the year, and d{2} represents the last two digits of the year. ((0[1-9])|(1[0-2])) matches the month, ((0-2)|(3[0-1])) matches the day, d{3}[0-9xX] Match the sequence code and check code, where x and X indicate that the check code can be numbers 0-9 or the letter X (both upper and lower case).

In golang, you can use the regexp package for regular expression matching. The specific usage is as follows:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    var id string = "110101199003079077"

    // 定义正则表达式
    reg := `^([1-9]d{5})(19|20)d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|(3[0-1]))d{3}[0-9xX]$`

    // 编译正则表达式
    rgx := regexp.MustCompile(reg)

    // 进行匹配
    match := rgx.Match([]byte(id))

    if match {
        fmt.Println("身份证号码合法")
    } else {
        fmt.Println("身份证号码非法")
    }
}
Copy after login

In the above code, an ID card number id is first defined, and then the Compile function in the regexp package is used to compile the regular expression into a regular object rgx. Finally, call the rgx.Match function to match the ID number. If the match is successful, it will return true, otherwise it will return false.

3. Summary

Through the above introduction, we can see that it is very easy to use regular expressions to verify the legitimacy of the ID number in golang. You only need to define a regular expression, and then use the regexp package to compile it into a regular object for matching. Of course, since ID number rules may have some changes in different scenarios, the regular expression needs to be adjusted according to specific needs.

The above is the detailed content of Use regular expressions in golang to verify whether the input is a legal identity document number. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template