2

I have a string of images' URLs and I need to convert it into an array.

http://rubular.com/r/E2a5v2hYnJ

How do I do this?

1
  • Why does it have to use a regular expression? Commented Aug 16, 2011 at 22:16

5 Answers 5

5
URI.extract(your_string)

That's all you need if you already have it in a string. I can't remember, but you may have to put require 'uri' in there first. Gotta love that standard library!

Here's the link to the docs URI#extract

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

1 Comment

Nice! Didn't know about that.
4

Scan returns an array

myarray = mystring.scan(/regex/)

See here on regular-expressions.info

Comments

1

The best answer will depend very much on exactly what input string you expect.

If your test string is accurate then I would not use a regex, do this instead (as suggested by Marnen Laibow-Koser):

mystring.split('?v=3')

If you really don't have constant fluff between your useful strings then regex might be better. Your regex is greedy. This will get you part way:

mystring.scan(/https?:\/\/[\w.-\/]*?\.(jpe?g|gif|png)/)

Note the '?' after the '*' in the part capturing the server and path pieces of the URL, this makes the regex non-greedy.

The problem with this is that if your server name or path contains any of .jpg, .jpeg, .gif or .png then the result will be wrong in that instance.

Figuring out what is best needs more information about your input string. You might for example find it better to pattern match the fluff between your desired URLs.

2 Comments

@km be careful with the character class [\w.-\/] you are creating a range from . to /. Here its not the problem that you include characters you don't want, because the slash is following directly after the dot in the ascii table. BUT the - is here a range operator and not the character. This class will not match the - literally. You have to escape the minus or put it to the start or the end like this [\w.\/-]
You've taken my suggestion wrong. I was assuming split would be used with a regex.
1

Use String#split (see the docs for details).

Comments

-1

Part of the problem is in rubular you are using https instead of http.. this gets you closer to what you want if the other answers don't work for you:

http://rubular.com/r/cIjmjxIfz5

1 Comment

Have you an idea, what the expression https? is doing? The ? makes the s optional, so https? will match http and https

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.