2つの便利なjavascript正規表現が見つかりましたが、どれもSwiftで正しく動作していないようです。Vimeo&Dailymotion IDを取得するためのREGEX
これはYoutube URLで動作しますが、VimeoやDailyMotionでは動作しません。どちらもVimeo & Dailymotion regexはjavascriptで動作します。
ありがとうございます。溶液で
** EDIT **
おかげGreatBigBoreとLarme
private func getVendorId(_ regexStr:String, _ videoUrl:String, _ search:[Int]) -> String {
let regex = try? NSRegularExpression(pattern: regexStr, options: .caseInsensitive)
let result:[NSTextCheckingResult] = (regex?.matches(in: videoUrl, options: [], range: NSRange(location: 0, length: (videoUrl.characters.count))))!
if result.count > 0 {
let groups = result.first!;
for i in search {
if (groups.rangeAt(i).length != 0) {
return (videoUrl as NSString).substring(with: groups.rangeAt(i))
}
}
}
return "";
}
override func viewDidLoad() {
let youtubeUrl = "https://www.youtube.com/watch?v=MCc0V5bgEF8";
let youtubeRegex = "((?<=(v|V)/)|(?<=be/)|(?<=(\\?|\\&)v=)|(?<=embed/))([\\w-]++)";
let youtubeId = getVendorId(youtubeRegex, youtubeUrl, [0]);
print("YOUTUBE ID -> \(youtubeId)")
let vimeoUrl = "https://player.vimeo.com/video/233459603";
let vimeoRegex = "https?:\\/\\/(?:www\\.|player\\.)?vimeo.com\\/(?:channels\\/(?:\\w+\\/)?|groups\\/([^\\/]*)\\/videos\\/|album\\/(\\d+)\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)";
let vimeoId = getVendorId(vimeoRegex, vimeoUrl, [2]);
print("VIMEO ID -> \(vimeoId)")
let dailyMotionUrl = "http://www.dailymotion.com/hub/x9q_Galatasaray";
let dailyMotionRegex = "^.+dailymotion.com\\/(video|hub)\\/([^_]+)[^#]*(#video=([^_&]+))?";
let dailyMotionId = getVendorId(dailyMotionRegex, dailyMotionUrl, [4, 2]);
print("DAILYMOTION ID -> \(dailyMotionId)")
super.viewDidLoad()
}
このコードはコンパイルされません。私は助けたいと思いますが、実際のコードを貼り付ける必要があります。私はあなたの関数定義の2番目の 'regstr'を' url'に変更しましたが、私は気付きました。実際のコードを貼り付けてみてください。 – GreatBigBore
コードを編集しました、ありがとう! – noisedan
'let regRes:[Any]'代わりに 'let regRes:[NSTextCheckingResult]'を書くと 'as? 'regRes.first as 'のNSTextCheckingResult'は? NSTextCheckingResult'です。残りの部分については、 'regRes'の値を最初にチェックするのではなく、すべてチェックしましたか?彼らは何を印刷しますか?あなたは様々な正規表現グループを持っているので、どの正規表現が各正規表現に対して有効であるかを見つけることは興味深いかもしれません。 – Larme