Go (RE2)

URL Validation in GO

Match http and https URLs with optional www prefix, paths, query strings, and fragments.

Try it in the GO tester →

Pattern

regexGO
https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*   (flags: gi)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`(?i)https?:\/\/(?:www\.)?[\w\-]+(?:\.[\w\-]+)+[\w\-._~:/?#\[\]@!$&'()*+,;=%]*`)
	input := `https://www.example.com`
	for _, match := range re.FindAllString(input, -1) {
		fmt.Println(match)
	}
}

Uses `regexp.MustCompile` (panics on bad patterns at startup) and `FindAllString` for all matches.

How the pattern works

Starts with http:// or https://, optional www., then one or more domain labels separated by dots, followed by any valid URL characters for the path and query.

Examples

Input

https://www.example.com

Matches

  • https://www.example.com

Input

http://api.example.org/v1/users?id=42

Matches

  • http://api.example.org/v1/users?id=42

Input

not a url

No match

Same pattern, other engines

← Back to URL Validation overview (all engines)