[Tutor] String searching

Emile van Sebille emile@fenx.com
Wed, 31 May 2000 05:56:30 -0700


>>> word = "http://www.fenx.com"
>>> import string
>>> if string.split(word,'://')[0] in ('http','ftp'):
 print word , ' is a url.

or, in 1.6:

>>> if word.split('://')[0] in ('http','ftp'):
 print word,'is a url'


Emile van Sebille
emile@fenx.com
-------------------


----- Original Message -----
From: André Dahlqvist <andre@beta.telenordia.se>
To: Python Tutor Mailing List <tutor@python.org>
Sent: Wednesday, May 31, 2000 2:42 AM
Subject: [Tutor] String searching


> Hi everyone,
>
> I'm trying to find out if a certain word in a string is a hyperlink,
in
> other words if it starts with http:// or ftp://. I've come up with a
> way that works, but I think there must be some less uglier way of
doing
> this. Here's the solution I use now:
>
> for word in string.split(text):
> if "http://" == word[0:7] or "ftp://" == word[0:6]
> do stuff with word...
>
> The test itself is what I think is ugly, and I would have prefered to
> use something like:
>
> if "http://" or "ftp://" in string.split(text):
> get the whole word that matched...
>
> But I don't know how to get this whole match. I would be greatful for
> any advice.
> --
>
> // André
>
> ===================================================================
> André Dahlqvist <andre@beta.telenordia.se>
> GnuPG Key ID: 0x70A2994A
> Fingerprint: E947 3297 331C CA30 5B88  EDF2 A830 3EBE 70A2 994A
> ===================================================================
>
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://www.python.org/mailman/listinfo/tutor
>