问题
In a system that I'm developing I need to recognize the youtube link in the following format
[youtube]youtube url[/youtube]
for the moment I arrived at this regular expression:
#\[youtube\]http://www.youtube\.(.*)/watch\?v=([a-zA-Z0-9_-]*)\[\/youtube\]#s
But this pattern isn't able to recognize url like
[youtube]http://www.youtube.com/watch?v=s3wXkv1VW54&feature=fvst[/youtube]
Note the feature=fvst.
Some one can help me in recognize all possible youtube url?
回答1:
How about
|\[youtube\]http://www.youtube\.(.*?)/watch\?v=([a-zA-Z0-9_-]*)[%&=#a-zA-Z0-9_-]*\[\/youtube\]|s
EDIT: Changed the regex to keep only the video id inside parentheses.
回答2:
Notes:
I'd perform a case-insensitive match on URLs (/i)
. matches anything; use \. instead to match the URL
Also, "www." in Youtube URLs is optional.
Use (\.[a-z]+){1,2} instead of ".*" to match ".com", ".co.uk", .. after "youtube"
Assuming the "&feature" part is optional, the regex would be:
/\[youtube\]http:\/\/(www\.)?youtube(\.[a-z]+){1,2}\/watch\?v=([a-z0-9_-]*)(&feature=([a-z]+))?\[\/youtube\]/is
Assuming the "&feature" part is optional, AND there can be other parameters than "feature":
/\[youtube\]http:\/\/(www\.)?youtube(\.[a-z]+){1,2}\/watch\?v=([a-z0-9_-]*)(&([a-z]+)=([a-z0-9_-])+)*?\[\/youtube\]/is
来源:https://stackoverflow.com/questions/2220764/reg-exp-for-youtube-link