regular expression question

Mark McEahern mark at mceahern.com
Tue Jan 20 20:33:51 EST 2004


On Tue, 2004-01-20 at 18:38, Steve Lamb wrote:
>     Ok, this just seems convluted.
> 
> import re
> line = 'match this'
> foo = re.search(r'.*(this)', line)
> foo.groups()[0]
> 
>     To me there seems there should be a way to return a group without having
> to get the tuple and then slice it out from there.  However foo.group(0) will
> return the entire string (only one argument given) and groups doesn't seem to
> do anything significant with any entry I give it.  Am I missing something
> obvious or is this the final solution?

Have you bothered to read the docs on Match objects?

  import re
  line = 'match this'
  match = re.search(r'.*(this)', line)
  print match.group(1)

// m





More information about the Python-list mailing list