I asked a similar question earlier for which Nokogiri was recommended as a solution. I've used Nokogiri and it certainly works fine.
But due to certain reasons, I must use regex to extract a keyword from a HTTP response body.
Format of the keyword is as follows:
<HTML>
<HEAD> <TITLE>TestExample [Date]</TITLE></HEAD>
</HTML>
Here, Date
is a dynamic variable, and I need to extract 'TestExample [Date]
' from the HTTP response body. Also, <title>
can be lower or upper case.
Assuming 'response' has the http response, I have tried doing the following:
>> response
=> "<HTML>\n<HEAD> <TITLE>TestExample [Date]</TITLE></HEAD>\n</HTML>"
Then make a regex to search:
>> regex
=> /<title>TestExample (.*?)<\/title>/mi
When I do a response[regex]
there are no results. No results with response.match(regex)
and response.scan(regex)
.
How can I do this task using regex?
Update:
For this task, this regex works fine:
response.match(/<title>(.*)<\/title>/mi).captures.first