Question

I'm making and I made it to work initially, only because I lucked out in that the videos I was testing with were valid. Allow me to explain further.

What I'm trying to do is to upload vimeo content. Usually in vimeo's url the number portion consists of either 7 or 8 digits which I am attempting to capture and place them in my show and index.html. I originally made it work and I thought it was fully functional but as I tried to upload more videos, it turns out that my regex in either my model file or my index is only allowing for one digit to be captured. At the beginning videos would play because a url with one digit happened to be valid. So what I want to know is what I'm doing wrong. I tried a few things on rubular but I suspect that the regexes that I'm coming up with don't do what I think they're doing. Here is what I have:

validates :url_id, format: { with: /\Ahttp:\/\/vimeo.com\/\d+/ }
validates :url_id, format: { with: /\Ahttp:\/\/vimeo.com\/\d{7,8}/ }

The above code is in my model file. The first line of code should work. I want the user to copy paste a url in this format and because a vimeo url has 7 or 8 digits in its url, this should validate the url (I think, Rubular confirmed it for me).

In my index.html and show.html, I have this:

<iframe src="http://player.vimeo.com/video/<%=/[0-9]/.match(music_video.url_id)%>"

putting the [0-9] in rubular tells me that it should capture all the numbers in the url, but its only capturing one digit, the first digit. Am I missing something? does \d+ does something I don't know about?

Was it helpful?

Solution

Regex 101:

[0-9]    matches a single character, which must consist of the digits 0 through 9
[0-9]+   matches 1 or more characters, all of which must be the digits 0 through 9
[0-9]*   matches 0 or more characters, all of which must be the digits 0 through 9
[0-9]{n,m} matches at least 'n' chars, up to 'm' chars, all which must be 0 through 9
[0-9]{n,} matches at least 'n', with no upper limit, etc...
[0-9]{,m} matches at MOST 0 up to 'm' worth of characters.

which implies the following equivalencies:

[0-9]          [0-9]{1}
[0-9]*         [0-9]{0,}
[0-9]+         [0-9]{1,}

and \d is a convenience, a shortcut which is equivalent to [0-9] and just means "digits".

OTHER TIPS

Rubular will higlight all the sequences that match. In your example it would look like it matches the whole number where it just matches all the numbers individually. You will figure this out easily if you put ([0-9]) as your pattern in rubular.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top