0

I wanted to get image url "http://www.test.com/image.jpg" out from the string:

"<img align="right" alt="Title " src="http://www.test.com/image.jpg" width="120" /><"

Here is the code that I have:

module MyHelper

    def getMymage(allDesc)
        allDesc = "<img align="right" alt="Title " src="http://www.test.com/image.jpg" width="120" /><"
        allDesc = allDesc.scan(src="(\S+)")
    end
end

I got the following error:

syntax error, unexpected tAMPER
        allDesc = allDesc.scan(src="(\S+)")
                                    
syntax error, unexpected $undefined
        allDesc = allDesc.scan(src="(\S+)")
                                       

How to fix it?

1
  • Please correct the encoding of pasted text, it would be much easier to help you then! Commented Sep 4, 2011 at 18:00

2 Answers 2

3

Can't comment on sunkencity's answer, but regex that solves the dash problem is:

/src=\"([a-z0-9_.\-:\/]+)"/i
Sign up to request clarification or add additional context in comments.

2 Comments

i tried allDesc =~ /src=\"([a-z0-9_.\-:\/]+)"/i but it doesn't return anything?
even with this: allDesc =~ /src=\"([a-z0-9_.\-:\/]+)"/i && $1
2

The regexp is missing a start "/" and some extra stuff

allDesc.scan(/src=\"([a-z0-9_.\-:\/]+)"/i)

but you get an array as a response:

=> [["http://www.test.com/image.jpg"]] 

I'd suggest using the matching operator and then use the first match variable:

allDesc =~ /(http:\/\/[a-z0-9_.-i\/]+)/ && $1

7 Comments

Thanks. almost there. Just a little bug. for image url like: test.com/images/1-2-3.jpg, it returns the url: test.com/images/1
I modified it a bit: allDesc =~ /(http:\/\/\S+)/ && $1 , but there is quotation at the end.
Just put a & outside your matching group like this allDesc =~ /(http:\/\/\S+)&/ && $1 => "test.com/image.jpg"
allDesc =~ /(http:\/\/\S+)&/ && $1 doesn't return anything tho
does allDesc still contain the string? works for me: ree-1.8.7-2010.02 :043 > allDesc => "<img align="right" alt="Title " src="test.com/image.jpg" width="120" /><" ree-1.8.7-2010.02 :044 > allDesc =~ /src=\"([a-z0-9_.\-:\/]+)"/i && $1 => "test.com/image.jpg"
|

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.