Regex is the wrong tool for handling HTML (or XML) 99.9% of the time. Instead, use a parser, like Nokogiri:
require 'nokogiri'
html="<img src="https://filin.mail.ru/pic?width=90&height=90&email=multicc%40multicc.mail.ru&version=4&build=7" style="">"
doc = Nokogiri::HTML(html)
url = doc.at('img')['src'] # => "https://filin.mail.ru/pic?width=90&height=90&email=multicc%40multicc.mail.ru&version=4&build=7"
doc.at('img')['style'] # => ""
Once you’ve retrieved the data you want, such as the src
, use another “right” tool, such as URI:
require 'uri'
scheme, userinfo, host, port, registry, path, opaque, query, fragment = URI.split(url)
scheme # => "https"
userinfo # => nil
host # => "filin.mail.ru"
port # => nil
registry # => nil
path # => "/pic"
opaque # => nil
query # => "width=90&height=90&email=multicc%40multicc.mail.ru&version=4&build=7"
fragment # => nil
query_parts = Hash[URI.decode_www_form(query)]
query_parts # => {"width"=>"90", "height"=>"90", "email"=>"[email protected]", "version"=>"4", "build"=>"7"}
solved Can’t figure out why match result is nil [closed]