[Tutor] String searching

Remco Gerlich scarblac@pino.selwerd.nl
Wed, 31 May 2000 12:43:04 +0200


On Wed, May 31, 2000 at 11:42:30AM +0200, André Dahlqvist wrote:
> 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.

In Python 1.6, you will be able to use if word.startswith("http://"): ...
That would look a bit nicer, but isn't available yet.

Otherwise your current solution is probably the easiest for this case.

If you want to make it more generic, you could do something like

   i = string.find(word, '://')
   if i >= 0 and word[:i] in 'http','https','ftp','telnet','mailto','news':
      do stuff with word...
	  
There are actually a lot more types of URIs and they don't all use '://',
but this sort of thing is good enough for most purposes, I suppose :)

-- 
Remco Gerlich,  scarblac@pino.selwerd.nl