[Tutor] Simple reg-ex syntax?

Allen Fowler allen.fowler at yahoo.com
Thu Mar 13 19:43:52 CET 2008


findall is great.  Thank you. :)

----- Original Message ----
> From: Chris Fuller <cfuller084 at thinkingplanet.net>
> To: tutor at python.org
> Sent: Thursday, March 13, 2008 5:10:43 AM
> Subject: Re: [Tutor] Simple reg-ex syntax?
> 
> 
> How I would improve this:
> 
> compile the regular expression.  This is more efficient.
> self.digit_extractor = re.compile('(\d+)')
> 
> then, use the findall method:
> self.allNumbers = self.digit_extractor.findall(self.aString)
> which will even work with multiline strings, but doesn't convert to integers.
> 
> To convert, use a list comprehension:
> self.allNumbers = [int(i) for i in self.digit_extractor.findall(self.aString)]
> 
> Cheers
> 
> On Wednesday 12 March 2008 21:59, Allen Fowler wrote:
> > Hello,
> >
> > I have code that looks something like:
> >
> > self.aString = "abc123xyz"
> > self.theNumber = int(re.search('(\d+)',self.aString).group())
> >
> > Is there a more Pythonic way of doing this? (Both the reg-ex and the Int
> > coercion.)  How about when I will need to extract more than one substring?
> >
> > Thank you,
> >
> > :)
> >
> >      
> > ___________________________________________________________________________
> >_________ Never miss a thing.  Make Yahoo your home page.
> > http://www.yahoo.com/r/hs
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 




      ____________________________________________________________________________________
Looking for last minute shopping deals?  
Find them fast with Yahoo! Search.  http://tools.search.yahoo.com/newsearch/category.php?category=shopping



More information about the Tutor mailing list