2012-06-12 40 views
1

我试图使用提取自YouTube网址视频ID如下:斯卡拉正则表达式的YouTube视频ID

val YoutubeRegex = """v=([^&]+)""".r 

    "v=IQJ13vFYOU8&feature=g-all-lik" match { 
    case YoutubeRegex(videoId) => videoId 
    case _ => throw new NoSuchFieldError("impossible to find youtube Id") 
    } 

Saddly这不工作...任何想法?非常感谢

回答

2

是不是应该这样?

val YoutubeRegex = """v=([^&]+).*""".r // need to specify that there could be remainder 

"v=IQJ13vFYOU8&feature=g-all-lik" match { 
    case YoutubeRegex(videoId) => videoId 
    case _ => throw new NoSuchFieldError("impossible to find youtube Id") 
} 

所以你会得到IQJ13vFYOU8部分,而不选择。

+0

这样做,奇怪我在F#中记住正则表达式不一定是“完整的”。在任何情况下多谢 – jlezard

+0

这是查找和匹配之间的区别。匹配应匹配整个字符串。 – user482745

相关问题