[英]Ruby on Rails string scan and regular expression
I wanted to get image url "http://www.test.com/image.jpg" out from the string: 我想从字符串中获取图片网址“ http://www.test.com/image.jpg”:
"<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+)") 我收到以下错误:语法错误,意外的tAMPER allDesc = allDesc.scan(src =“(\\ S +)”)
syntax error, unexpected $undefined allDesc = allDesc.scan(src="(\\S+)") 语法错误,意外$ undefined allDesc = allDesc.scan(src =“(\\ S +)”)
How to fix it? 如何解决?
Thanks in advanced. 提前致谢。
无法评论sunkencity的答案,但是解决破折号问题的正则表达式是:
/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: 我建议使用匹配运算符,然后使用第一个match变量:
allDesc =~ /(http:\/\/[a-z0-9_.-i\/]+)/ && $1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.