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

2
  • 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? Commented Mar 19, 2018 at 9:44
  • Edited for viemo Url. Commented Mar 19, 2018 at 9:46

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly the solution I was looking for. Thank you.
4

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.