Dennis Shy
Dennis Shy

Reputation: 79

Regular Expression - matching file extension from a URL

So I have a very specific URL, that tends to always follow the following format:

http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32

Basically I want to grab everything from after the . and before the ?versionId as I imagine that's the consistent location of the file extension.

I currently have something like this where \.\.{0}(.+)\?versionId it is matching everything starting from the first . to versionId.

One solution I thought about doing was using the . as a delimiter. I've never tried to restrict a character, but basically I would want it to try to match everything starting with a ., reject anything that has a . leading up to the ?.

Anyone got any idea how to get this to work?

Upvotes: 0

Views: 828

Answers (2)

Amadan
Amadan

Reputation: 198324

Completely in agreement with Philip Hallstrom, this is a typical XY problem. However, if you really wish to just hone your Regexp skills, the literal solution to your question is (Rubular):

(?<=\.)[^.]+(?=\?)

"From where a period was just before, match any number of non-periods, matching up to where question mark is just after."

To understand this, read up on positive lookbehind ((?<=...)), positive lookahead ((?=...)), and negated character sets ([^...]).

Upvotes: 0

Philip Hallstrom
Philip Hallstrom

Reputation: 19879

Is your goal to get 'mp4'? Might consider not using a regex at all...

> require 'uri'
> uri = URI.parse('http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32')
=> #<URI::HTTP http://mtc.cdn.vine.co/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4?versionId=.k9_w6W7t1Yr1KUCWRIm6AnYhSdOUz32>
> uri.path
=> "/r/videos/0DCB6FF2EF1179983941847883776_38a153447e7.1.5.3901866229871838946.mp4"
> File.extname(uri.path)
=> ".mp4"

Upvotes: 4

Related Questions