regex to separate HTML GET parameters

荒凉一梦 提交于 2019-12-11 13:43:22

问题


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?


回答1:


A simplified regex :

^(?:http://www.)?youtube.[^/]+?/watch?(.?)(v=([^&]+))(.)$




回答2:


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-]+)



来源:https://stackoverflow.com/questions/12812985/regex-to-separate-html-get-parameters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!