How to extract some text?

Tim Pinkawa timpinkawa at gmail.com
Sun Mar 8 18:42:46 EDT 2009


On Sun, Mar 8, 2009 at 5:18 PM, Oltmans <rolf.oltmans at gmail.com> wrote:
> I'm at a loss to figure out how to extract some text from a string.
> Here is a string:
>
> setTimeout("location.href='http://youtube.example.com/login.aspx'",
> 5000);
>
> and I want to only retrieve the URL from above i.e I only want this
> http://youtube.example.com/login.aspx from the above string. Any ideas/
> help is highly appreciated.
>
> Thanks,
> Oltmans

If x is your string:
>>> pos = x.find("'") + 1
>>> x[pos:x.find("'", pos)]
'http://youtube.example.com/login.aspx'

Find the first single quote, then get the character range between that
and the next single quote.

Tim



More information about the Python-list mailing list