[Tutor] How can this code be made even better ?
Alan Gauld
alan.gauld at btinternet.com
Thu Aug 6 12:06:13 CEST 2015
On 06/08/15 06:39, Aadesh Shrestha wrote:
> import re
>
> text = input('Enter your text with phone number using xx-xxxxxxx format \n')
> contact = re.compile(r'\d\d-\d\d\d\d\d\d\d')
>
> for i in range(len(text)):
> chunk = text[i:i+10]
> mo = contact.search(chunk)
> if mo:
> print('Phone Number Found:'+mo.group())
Remove the loop?
re.search can find the pattern in the whole of text
without a loop. That's basically the point of it.
--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos
More information about the Tutor
mailing list