开发者

Ruby on Rails string scan and regular expression

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

"<img align=&qu开发者_开发技巧ot;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?


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

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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜