Download Cheat sheet PDF 12 pages · syntax, editors, patterns, Unicode, performance, debugging
Pattern

Regex for Spotify URI

spotify:track/album/playlist URI.

The Spotify URI regex is ^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$ — copy it below, or open it in the explainer for a token-by-token breakdown.

The pattern

^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$
Try in explainer → Download cheat sheet ↓

What it matches

  • spotify:track:4uLU6hMCjMI75M1A2tKUQC
  • spotify:playlist:37i9dQZF1DXcBWIGoYBM5M
  • spotify:artist:1Xyo4u8uXC1ZmMpatF05PJ

What it doesn't match

  • spotify:track:short
  • spotify:song:abc
  • https://spotify.com/track/abc

Notes & gotchas

Spotify URIs identify content by type and 22-char Base62 ID. Used in deep links and the Spotify API. Web URLs (open.spotify.com/track/...) use the same ID.

Code in your language

Use the explainer's Code tab to generate ready-to-paste snippets in JavaScript, Python, Java, .NET, Go, Ruby, and PHP for this pattern.

Open in explainer →

Token-by-token breakdown

Every part of the pattern, left to right:

TokenMeaning
^start of string (or line in multiline mode)
spotify:literal text “spotify:”
(start of a capturing group
trackliteral text “track”
|OR — try the alternative
albumliteral text “album”
|OR — try the alternative
playlistliteral text “playlist”
|OR — try the alternative
artistliteral text “artist”
|OR — try the alternative
episodeliteral text “episode”
|OR — try the alternative
showliteral text “show”
)end of group
:literal text “:”
[A-Za-z0-9]{22}exactly 22 times: any of: uppercase letters, lowercase letters, digits
$end of string (or line in multiline mode)

About this pattern

Identifying and validating user identity (names, contact info, IDs) is one of the most common reasons developers reach for regex. The pattern below handles the format check; for full validation always confirm against a source of truth (database, API, or document).

Quick usage in different languages

This exact pattern — with the correct escaping and idioms for each language:

  • JavaScript: /^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$/.test(value)
  • Python: re.match(r"^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$", value)
  • Java: Pattern.compile("^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$").matcher(value).matches()
  • C# / .NET: Regex.IsMatch(value, @"^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$")
  • Go: regexp.MustCompile(`^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$`).MatchString(value)
  • Ruby: /^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$/.match?(value)
  • PHP: preg_match('~^spotify:(track|album|playlist|artist|episode|show):[A-Za-z0-9]{22}$~', $value)

The explainer’s Code tab regenerates these for any pattern you paste, and the downloadable cheat sheet bundles the breakdown, all seven snippets, and the pitfalls below onto one printable page.

Common pitfalls

  • Anchored to the whole string. This pattern uses ^ and $, so it requires the entire input to match. To find it inside a longer text, drop the anchors and use the global (g) flag.
  • ASCII letters only. [a-zA-Z] excludes accented and non-Latin letters (é, ü, ß, ñ, and non-Latin scripts). For international input use Unicode properties like \p{L} with the u flag.
  • Validate beyond format. Matching the format doesn't guarantee the value is real. Confirm the spotify uri against a source of truth (database, API, or checksum) where it matters.

Related patterns

More patterns in the Identity & contact category:

See also

Browse all 300 patterns in the library, or open this regex in the interactive explainer to see a token-by-token breakdown, test against custom input, and generate code in seven languages.


← Back to all patterns