Web addresses
A regex exercise from Regexdrill. Solve it with a pattern box with live highlighting in your browser, checked against hidden test strings, and it is checked the moment you submit.
The task
The whole line must match. Use this simplified rule: http or https, then ://, then a host of two or more labels separated by dots (each label is letters, digits or hyphens), then optionally a slash followed by any characters except whitespace.
What this exercise teaches
https?://[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+(?:/\S*)? spells out each part of the rule. The plus after the dot-and-label group is what demands at least two labels, so https://example fails. Real URLs allow more (ports, user names, international names), so in production code use a URL parser and keep a regex for quick checks.
A hint if you get stuck
s? makes the s optional. Build the host as one label followed by one or more groups of a dot and a label. The path is an optional group that starts with a slash.
Try it now, free, no account needed
Every exercise on Regexdrill is free to try. Open it on the practice page, solve it, and get instant feedback with a hint. The worked solution opens once you solve it or after two tries.
Open this exerciseMore exercises in Real-world validation
- US ZIP and ZIP+4Free
- US phone number formatsFree
- Dates YYYY-MM-DDFree
- 24-hour times HH:MMFree
- 12-hour times with AM or PMFree
- Simple email addressesFree
- Hex colorsFree
- IPv4 addresses, 0 to 255Free
- URL slugsFree
- UsernamesFree
- SKU codesFree
- Card numbers in groups of fourFree
- Currency amounts with commasFree
- Decimal numbersFree
- Semantic versionsFree
- MAC addressesFree
- UUIDsFree
- Percentages from 0 to 100Free
- ISO 8601 timestampsFree