Go (RE2)

URL Query String in GO

Extract the query string portion of a URL (everything between `?` and `#` or end-of-string).

Try it in the GO tester →

Pattern

regexGO
\?([^#\s]+)   (flags: g)

Go (RE2) code

goGo
package main

import (
	"fmt"
	"regexp"
)

func main() {
	re := regexp.MustCompile(`\?([^#\s]+)`)
	input := `https://example.com/page?utm_source=google&id=42`
	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

\? matches the literal question mark. ([^#\s]+) captures everything up to (but not including) a `#` (fragment) or whitespace (URL boundary). Use URL parsing libraries for production, but this regex is handy for quick log scraping.

Examples

Input

https://example.com/page?utm_source=google&id=42

Matches

  • ?utm_source=google&id=42

Input

/api?token=abc#section

Matches

  • ?token=abc

Input

no query string here

No match

Same pattern, other engines

← Back to URL Query String overview (all engines)