chandrashekar
chandrashekar

Reputation: 619

how to extract url from html body in ruby

I need to extract URL from HTML body, this is my HTML body

"<html><head><meta http-equiv=refresh content=0;URL=/ref.php?offer_id=350&aff_id=28&url=https%3A%2F%2Fplay.leadzuaf.com%2F%3Fm%3D0ENYJG473721%26offer_key%3D473721%26fc%3D1%26a%3Dy00704Hj50h1zF05XC0HZEp0Kpefss.%7Bpubid%7D%26pubid%3D28&urlauth=ab27ecac97f1760d912ad169b4af1e4b></head></html>"

if I am extract this string using URI.extract(str) I am getting an empty array. please help me how to extract this

Upvotes: 1

Views: 250

Answers (2)

Rajagopalan
Rajagopalan

Reputation: 6064

a="<html><head><meta http-equiv=refresh content=0;URL=/ref.php?offer_id=350&aff_id=28&url=https%3A%2F%2Fplay.leadzuaf.com%2F%3Fm%3D0ENYJG473721%26offer_key%3D473721%26fc%3D1%26a%3Dy00704Hj50h1zF05XC0HZEp0Kpefss.%7Bpubid%7D%26pubid%3D28&urlauth=ab27ecac97f1760d912ad169b4af1e4b></head></html>"

p a[/URL=([^>]*)/,1]

#=>"/ref.php?offer_id=350&aff_id=28&url=https%3A%2F%2Fplay.leadzuaf.com%2F%3Fm%3D0ENYJG473721%26offer_key%3D473721%26fc%3D1%26a%3Dy00704Hj50h1zF05XC0HZEp0Kpefss.%7Bpubid%7D%26pubid%3D28&urlauth=ab27ecac97f1760d912ad169b4af1e4b"

Upvotes: 0

Aleksei Matiushkin
Aleksei Matiushkin

Reputation: 121010

html = "<html><head><meta http-equiv=refresh content=0;URL=/ref.php?offer_id=..."
html[/(?<=URL=).*?(?=>)/]
#⇒ "/ref.php?offer_id=....."

Upvotes: 1

Related Questions