0

I want to extract Vimeo Id from its URL. I have tried to many solutions but not found what I exactly want for swift. I refer to many questions and found one solution in JAVA. I want same behaviour in iOS swift so I can extract the ID from matched group array.

Using Regular Expressions to Extract a Value in Java

I use following vimeo URL regex and I want group-3 if string matched with regex:

"[http|https]+:\/\/(?:www\.|player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)([a-zA-Z0-9_\-]+)(&.+)?"

Test Vimeo URL: https://vimeo.com/62092214?query=foo

Anup Kanjariya
  • 292
  • 1
  • 2
  • 15
  • It might be usefull to give some Vimeo URL to validate and check if your Regex is good, or where it's wrong. And what does currently your regex? Does it work? Is your issue just retrieving some info? What's your code? If it works, did you check `rangeAtIndex:` of `NSTextCheckingResult`? – Larme Mar 19 '18 at 09:44
  • Edited for viemo Url. – Anup Kanjariya Mar 19 '18 at 09:46

2 Answers2

8
let strToTest = "https://vimeo.com/62092214?query=foo"

let pattern = "[http|https]+:\\/\\/(?:www.|player.)?vimeo.com\\/(?:channels\\/(?:\\w+\\/)?|groups\\/([^\\/]*)\\/videos\\/|album\\/(\\d+)\\/video\\/|video\\/|)([a-zA-Z0-9_\\-]+)(&.+)?"

let regex = try! NSRegularExpression.init(pattern: pattern, options: [])
let match = regex.firstMatch(in: strToTest, options: [], range: NSRange.init(location: 0, length: strToTest.count))
let goup3Range = match?.range(at: 3)
let substring = (strToTest as NSString).substring(with: goup3Range!)
print("substring: \(substring)")

That should work.

You need to escape all \ in the pattern.
You need to call range(at:) to get the range of the group you want according to your pattern (currently group3), then substring.

What should be improved?
Well, I did all sort of force unwrapped (every time I wrote a !). for the sake of the logic and not add do/catch, if let, etc. I strongly suggest you check them carefully.

Larme
  • 24,190
  • 6
  • 51
  • 81
3

Here is yet another version. I am using named capturing group, a bit different than the answer provided by Larme.

let regex = "[http|https]+:\\/\\/(?:www\\.|player\\.)?vimeo\\.com\\/(?:channels\\/(?:\\w+\\/)?|groups\\/(?:[^\\/]*)\\/videos\\/|album\\/(?:\\d+)\\/video\\/|video\\/|)(?<vimeoId>[a-zA-Z0-9_\\-]+)(?:&.+)?"

let vimeoURL = "https://vimeo.com/62092214?query=fooiosiphoneswift"

let regularExpression = try! NSRegularExpression(pattern: regex,
                                                options: [])

let match = regularExpression.firstMatch(in: vimeoURL,
                                         options: [],
                                         range: NSRange(vimeoURL.startIndex ..< vimeoURL.endIndex,
                                                        in: vimeoURL))

if let range =  match?.range(withName: "vimeoId"),
    let stringRange = Range(range, in: vimeoURL) {
    let vimeoId = vimeoURL[stringRange]
}

Also, please check that I have modified your regex a bit, such that everything else except vimeoId are non-capturing.

Sandeep
  • 20,908
  • 7
  • 66
  • 106