Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Created August 3, 2010 01:14
Show Gist options
  • Save huacnlee/505648 to your computer and use it in GitHub Desktop.
Save huacnlee/505648 to your computer and use it in GitHub Desktop.
# Ruby 正则匹配查找替换例子
body = "这里是内容<img src='uploads/a.jpg' />asdgasdgasdg<img src=\"uploads/b.jpg\" alt=\"\">这里是内容是哪的客观加拉塞克的构建"
regex = Regex.new("<(img.+?src\\=[\"|']{0,1})(.+?)>",[Regexp::EXTENDED,Regexp::IGNORECASE])
# 用 String.scan 搜索匹配字符串中的多个 img 标签
body.scan(regex).each do |m|
puts "matched: #{m.to_s}"
# m[0] 是正则里面的第一个()匹配项
puts m[0]
puts m[1]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment