A query string parameter
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
Capture the value of the id parameter of a URL in group 1. A parameter starts right after ? or &, and its value runs up to the next & or #, or to the end of the line. Parameters whose name only ends in id, like uid, do not count. The value can be empty.
What this exercise teaches
[?&]id=([^&#]*) ties the name to the start of a parameter, so uid=7 is skipped. The negated class stops the value at the next & or #. The star rather than a plus lets an empty value through.
A hint if you get stuck
Start with a class that holds the two characters a parameter can follow, [?&], then the literal id= and a negated class for the value.
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 Extraction and cleanup
- Log lines: level and messageFree
- key=value pairsFree
- The third CSV fieldFree
- HashtagsFree
- Host names from URLsFree
- File extensionsFree
- Trim whitespaceFree
- Collapse repeated spacesFree
- Reformat MM/DD/YYYY as YYYY-MM-DDFree
- Swap Last, First to First LastFree
- Split camelCase with underscoresFree
- Strip HTML tagsFree
- Keep only the digitsFree
- CSV fields with quotesFree
- Normalize phone numbersFree
- Link text inside <a> tagsFree
- Web server access logFree