How can I use a regular expression to separate GET
parameters in a URI and extract a certain one? Specifically, I'm trying to get just the v=
part of a YouTube watch URI. I've come up with youtube.com\/watch\?(\w+=[\w-]+&?)*(v=[\w-]+)&?*(\w+=[\w-]+&?)*
, but that looks awfully repetitive. Is there a better (shorter?) way to do this?
Asked
Active
Viewed 262 times
1

Blacklight Shining
- 1,468
- 2
- 11
- 28
-
“page parameters”—I'm not sure what these are called. – Blacklight Shining Oct 10 '12 at 06:08
-
I think they are called POST parameters, or can reasonably be called that. – Martin Green Oct 10 '12 at 06:11
-
1Actually, parameters in the url are `GET` parameters. If you submit a form, the resulting request most likely contains `POST` parameters with your input. – Arjan Oct 10 '12 at 06:24
-
@Arjan Fixed again! Thanks. XD – Blacklight Shining Oct 10 '12 at 06:27
-
2Official name would be [Query String](http://en.wikipedia.org/wiki/Query_string), by the way. You don't have to edit again - I'm sure everyone gets it `:)` – Kobi Oct 10 '12 at 06:30
-
Next, please look at one of these: http://stackoverflow.com/search?q=youtube+regex – Kobi Oct 10 '12 at 06:31
-
possible duplicate of [Reg exp for youtube link](http://stackoverflow.com/questions/2220764/reg-exp-for-youtube-link) – Kobi Oct 10 '12 at 06:32
-
@Arjan Thanks for correcting me - shows what I know about all this "web" stuff. :) – Martin Green Oct 10 '12 at 06:54
2 Answers
0
A simplified regex :
^(?:http://www.)?youtube.[^/]+?/watch?(.?)(v=([^&]+))(.)$

Stephan
- 41,764
- 65
- 238
- 329
0
I know there are a lot of similar questions out there, but none has quite what I wanted. I'm looking for something capable of pulling out just the video ID—regardless of whether it's first in the parameter list, last, or buried in between others. Nothing I've seen has worked quite like that yet.
For reference, I'm using this web app for testing, and this set of test URIs:
http://www.youtube.com/watch?v=XXXXXXXXXXX
http://www.youtube.com/watch?v=XXXXXXXXXXX&feature=results_video&playnext=1&list=XXXXXXXXXXXXXXXXXX
http://www.youtube.com/watch?feature=player_embedded&v=XXXXXXXXXXX#!
http://www.youtube.com/watch?annotation_id=annotation_xxxxxx&feature=iv&src_vid=XXXXXXXXXXX&v=XXXXXXXXXX
Fellow Stack Exchangers, I propose the following regular expression to solve this:youtube.com\/watch\?(\S*)v=([\w-]+)

Blacklight Shining
- 1,468
- 2
- 11
- 28