Human word reader

David Zaslavsky diazona at ellipsix.net
Sat May 15 17:13:10 EDT 2010


Here's my take on that:

 loc = re.search('for\s+(\w+)', string).group(1)

Not much different, really, but it does allow for multiple spaces (\s+) as 
well as requiring at least one character in the word (\w+), and I use a 
matching group to extract the location directly instead of splitting the 
string "by hand".

:) David

On Saturday 15 May 2010 8:38:01 am Xavier Ho wrote:
> On Sat, May 15, 2010 at 9:32 PM, timo verbeek 
<timoverbeek10 at gmail.com>wrote:
> > On May 15, 1:02 pm, timo verbeek <timoverbee... at gmail.com> wrote:
> > Place starts always with for
> 
> Okay, much better.
> 
> Given that constraint, it looks like regular expression can do the job. I'm
> not very experienced with regex, though.
> 
> \w* matches a whole word composed of letters and numbers by default.
> 
> >>> result = re.search('for \w*', 'Give me the weather for London please.')
> >>> result.group()
> 
> 'for London'
> 
> >>> result.group().split()[1]
> 
> 'London'
> 
> Cheers,
> Xav
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 198 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.python.org/pipermail/python-list/attachments/20100515/51b9ff5f/attachment-0001.sig>


More information about the Python-list mailing list