How to validate username using regular expression in Go?

WBOY
Release: 2024-06-03 09:48:57
Original
591 people have browsed it

To use regular expressions to verify usernames in Go, you first need to install the regexp package. Next define a regular expression to match valid usernames, for example, consisting of letters, numbers, and underscores, and between 3 and 25 characters in length. It is then compiled into a regular expression object using the regexp.MustCompile() function. Finally, use the MatchString() method to verify that the username matches the regular expression.

如何在 Go 中使用正则表达式验证用户名?

Verifying usernames using regular expressions in Go

Regular expressions are a powerful tool for matching text patterns. In Go, we can use the regexp package to handle regular expressions. This tutorial will show you how to use regular expressions to validate usernames in Go.

Install the regular expression package

First, you need to install the regexp package by running the following command:

go get golang.org/x/exp/regexp
Copy after login

Define username regular expression

Next, you need to define a regular expression to match valid usernames. A valid username usually consists of letters, numbers, and underscores, and is between 3 and 25 characters in length. The following regular expression will match a username like this:

const usernameRegex = `^[a-zA-Z0-9_]{3,25}$`
Copy after login

Verify username

With the regular expression in place, you can use the regexp.MustCompile() function to It compiles into a regular expression object:

import (
    "regexp"
)

var usernameRE = regexp.MustCompile(usernameRegex)
Copy after login

Now, you can use the MatchString() method to verify whether the username matches the regular expression:

func isValidUsername(username string) bool {
    return usernameRE.MatchString(username)
}
Copy after login

Practical case

Here is an example of how to use the previous function to verify a username in a Go program:

package main

import (
    "fmt"
    "regexp"
)

var usernameRE = regexp.MustCompile(`^[a-zA-Z0-9_]{3,25}$`)

func main() {
    // 一些有效的用户名
    validUsernames := []string{"john", "jane_doe", "123_abc"}

    // 一些无效的用户名
    invalidUsernames := []string{"", "john123_", "_username_"}

    for _, username := range validUsernames {
        if isValidUsername(username) {
            fmt.Printf("%s is a valid username\n", username)
        }
    }

    for _, username := range invalidUsernames {
        if !isValidUsername(username) {
            fmt.Printf("%s is an invalid username\n", username)
        }
    }
}
Copy after login

Output:

john is a valid username
jane_doe is a valid username
123_abc is a valid username
 is an invalid username
john123_ is an invalid username
_username_ is an invalid username
Copy after login

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

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!