Re: [Tutor] Simple reg-ex syntax?
findall is great. Thank you. :) - Original Message > From: Chris Fuller <[EMAIL PROTECTED]> > To: tutor@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@python.org > > http://mail.python.org/mailman/listinfo/tutor > ___ > Tutor maillist - Tutor@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 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
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@python.org > http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Simple reg-ex syntax?
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