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@python.org > http://mail.python.org/mailman/listinfo/tutor _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor