Python (re)

URL Query String in PY

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

Try it in the PY tester →

Pattern

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

Python (re) code

pyPython
import re

pattern = re.compile(r"\?([^#\s]+)")
input_text = "https://example.com/page?utm_source=google&id=42"
for m in pattern.finditer(input_text):
    print(m.group(0))

Stdlib `re` module — no third-party dependency. Works on Python 3.6+.

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)