How to use regular expressions in golang to verify whether the input is a complete URL address

PHPz
Release: 2023-06-24 13:30:10
Original
1509 people have browsed it

In golang, it is very important to use regular expressions to verify whether an input is a complete URL address. Regular expressions are a descriptive language that can be used to match text patterns in strings. When we need to verify whether an input conforms to a certain format, regular expressions can help us quickly determine whether the input is correct. So how to use regular expressions in golang to verify whether the input is a complete URL address? This article will explain it to you.

First of all, we need to understand the basic format of a URL address. A URL contains the following parts:

  • Protocol: (such as http, ftp, mailto).
  • Hostname: (e.g. www.example.com).
  • Port number: (for example 80).
  • Path: (for example/index.html).
  • Query string: (for example?id=1&name=Tom).
  • Fragment: (e.g. #top).

In these sections, the protocol and hostname must be included. Therefore, we can use regular expressions to verify whether the input string contains these two parts. We can use golang's built-in regular expression package regexp to match whether the input string matches the format we want.

The sample code is as follows:

package main

import (
    "fmt"
    "regexp"
)

func main() {
    str := "https://www.example.com/index.html?id=1&name=Tom#top"
    pattern := `^(http|https)://[a-zA-Z0-9]+(.[a-zA-Z0-9]+)+([a-zA-Z0-9-._?,'+/\~:#[]@!$&*])*$`

    match, err := regexp.MatchString(pattern, str)
    if err != nil {
        fmt.Printf("Error: %s
", err)
        return
    }

    if match {
        fmt.Println("Valid URL")
    } else {
        fmt.Println("Invalid URL")
    }
}
Copy after login

In this code, we use the regular expression ^(http|https)://[a-zA-Z0-9] (.[a-zA-Z0-9] ) ([a-zA-Z0-9-._?,' /\~:#[]@!$&*])*$ to match the input characters string. Among them, ^ represents the beginning of the string, $ represents the end of the string, http|https represents matching the http or https protocol, [a-zA- Z0-9] (.[a-zA-Z0-9] ) means matching the host name, [a-zA-Z0-9-._?,' /\~:#[]@! $&*] means matching other parts in the URL, such as paths, query strings, fragments, etc.

When the match is successful, "Valid URL" is output, otherwise "Invalid URL" is output.

It should be noted that this regular expression simply verifies whether the format of the URL is correct, but does not verify the existence of the URL. If you need to verify the existence of the URL, you need to use other methods.

In short, by using regular expressions to verify whether the input is a complete URL address, it can help us quickly determine whether the input conforms to a specific format. In golang, it is very simple to use the regexp package to verify regular expressions, just use the MatchString function.

The above is the detailed content of How to use regular expressions in golang to verify whether the input is a complete URL address. 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!