How to extract some text?

C

Chris Rebert

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.

Learn about the methods of the string class (str):
http://docs.python.org/library/stdtypes.html#id4

You'll probably be most interested in .split()

Cheers,
Chris
 
T

Tim Pinkawa

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
 
O

odeits

OK, thanks I got it. I was trying to use Regex but .split() just
worked like a charm. Thank you ;)

The regex you were lookingfor was probably something like p =
re.compile(r'href='(.*?)')

when you p.match(mystring) group(1) will be your url
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top