Pattern library / Validation
Regex to Validate a Username
Most first-draft username rules are either too loose (^[a-z0-9_]+$ happily accepts _admin) or they smuggle in a lookahead that RE2 rejects. This one enforces the usual policy — 3–20 characters, letters/digits/_/-, starting and ending alphanumeric — with plain classes only, so it runs in JavaScript forms, Python backends and GA4/BigQuery alike.
How the pattern works
^[a-zA-Z0-9]— first character must be a letter or digit, which is what rejects_userand-userwithout any lookahead.[a-zA-Z0-9_-]{1,18}— the body: 1 to 18 more characters, so the total lands between 3 and 20. Underscore and hyphen are fine anywhere inside.[a-zA-Z0-9]$— last character must be alphanumeric too, souser_anduser-fail. Together with the first character that is the whole 'no leading/trailing separator' rule, expressed without a single assertion.- Anchored at both ends, so it is a full-match validator: in
RegExp.test()and Pythonre.fullmatch()the whole input is checked. GA4 filters are full-match by default too, so the same pattern can be pasted there as-is.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
john_doe | Match | 8 chars, underscore in the middle | Try → |
web_dev_42 | Match | digits and underscores allowed inside | Try → |
Jean-Luc | Match | hyphen inside, letters at both ends | Try → |
user.name | No match | dot is not in the allowed set | Try → |
_admin | No match | cannot start with an underscore | Try → |
ab | No match | too short — minimum is 3 characters | Try → |
Where to use it
JavaScript
const USERNAME = /^[a-zA-Z0-9][a-zA-Z0-9_-]{1,18}[a-zA-Z0-9]$/;
USERNAME.test('john_doe'); // true
USERNAME.test('_admin'); // false
Python
import re
re.fullmatch(r'[a-zA-Z0-9][a-zA-Z0-9_-]{1,18}[a-zA-Z0-9]', 'Jean-Luc') # match
re.fullmatch(r'[a-zA-Z0-9][a-zA-Z0-9_-]{1,18}[a-zA-Z0-9]', 'user-') # None
BigQuery / GA4 (RE2)
SELECT username,
REGEXP_CONTAINS(username, r'^[a-zA-Z0-9][a-zA-Z0-9_-]{1,18}[a-zA-Z0-9]$') AS is_valid
FROM `project.dataset.users`
WHERE NOT REGEXP_CONTAINS(username, r'^[a-zA-Z0-9][a-zA-Z0-9_-]{1,18}[a-zA-Z0-9]$')
Common mistakes
- The classic
^[a-z0-9_]{3,16}$allows a leading underscore and an uppercase-less input fails silently for names likeJohn. Decide case policy first: allow both cases (as here) or force[a-z]everywhere — do not mix. - Dots: many platforms (Steam, GitHub-style emails) accept
.in usernames. If yours does, widen the body class to[a-zA-Z0-9._-]— but then keep the alphanumeric ends, or.johnslips through. - Length and uniqueness are not regex jobs.
{1,18}encodes 3–20 chars only; reserved names (admin,root), case-insensitive duplicate checks and existence checks belong in code after the match.
FAQ
Why not just use \w+?
\w is [A-Za-z0-9_] — no hyphen, no length limit, and it happily matches a single character. It also matches inside larger strings unless you add anchors. The explicit class plus anchors states the actual policy.
How do I allow 2–32 characters instead of 3–20?
Change only the quantifier: ^[a-zA-Z0-9][a-zA-Z0-9_-]{0,30}[a-zA-Z0-9]$ gives 2–32 total. Everything else stays; the ends still forbid leading/trailing separators.
Can I use this pattern in GA4?
Yes — it is pure RE2 (no lookahead, no backreference), so GA4 and BigQuery accept it. GA4 only matches, though: to flag invalid values you filter with does not match regex, or do the validation in BigQuery with REGEXP_CONTAINS as in the snippet above. See the match types guide for which screen offers what.
Related patterns
Mention Regex - Match @handles in Text · Slug Regex — Match lowercase-hyphen URL Slugs · Strict Email Regex — RFC-ish Validation — Test & Copy
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.