正则表达式来分隔 HTML GET 参数 [英] regex to separate HTML GET parameters
问题描述
如何使用正则表达式来分隔 URI 中的 GET
参数并提取某个参数?具体来说,我试图获取 YouTube 观看 URI 的 v=
部分.我想出了 youtube.com\/watch\?(\w+=[\w-]+&?)*(v=[\w-]+)&?*(\w+=[\w-]+&?)*
,但这看起来非常重复.有没有更好(更短?)的方法来做到这一点?
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?
推荐答案
我知道有很多类似的问题,但没有一个完全是我想要的.我正在寻找能够仅提取视频 ID 的东西——无论它是在参数列表中的第一个、最后一个还是隐藏在其他人之间.我所见过的还没有像这样的效果.
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.
作为参考,我使用这个网络应用进行测试,以及这组测试 URI:>
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
Stack Exchangers,我提出以下正则表达式来解决这个问题:youtube.com\/watch\?(\S*)v=([\w-]+)
Fellow Stack Exchangers, I propose the following regular expression to solve this:youtube.com\/watch\?(\S*)v=([\w-]+)
这篇关于正则表达式来分隔 HTML GET 参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!