Regexdrill

Level 3: Real-world validation Exercise 37 of 88 Free

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 exercise

More exercises in Real-world validation