[Tutor] Bundle help!

bhaaluu bhaaluu at gmail.com
Mon Jul 16 00:01:12 CEST 2007



Regular Expressions module.

On 7/15/07, Sara Johnson <sarliz73 at yahoo.com> wrote:
> ========================
> Just curious, but in this link ('
> http://xahlee.org/perl-python/sort_list.html ') you
> mentioned, what does the "re" part mean?  At first I thought it was the name
> of the list or 'return' but that's included later.
> ***************
> def myComp (x,y):
>     import re
>     def getNum(str): return float(re.findall(r'\d+',str)[0])
>     return cmp(getNum(x),getNum(y))
> li.sort(myComp)
> print li # returns ['web7-s.jpg', 'my23i.jpg', 'fris88large.jpg',
> 'my283.jpg']
> Thanks,
> Sara

Someone recently showed this to me:

$ python

>>> import re

>>> dir(re)

'VERBOSE', 'X', '__all__', '__builtins__', '__doc__', '__file__',
'__name__', 'compile',
'engine', 'error', 'escape', 'findall', 'finditer', 'match', 'purge',
'search', 'split', 'sub',
'subn', 'template']

>>> dir(re.match)

['__call__', '__class__', '__delattr__', '__dict__', '__doc__', '__get__',
'__getattribute__', '__hash__', '__init__', '__module__', '__name__',
'__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__str__',
'func_code', 'func_defaults', 'func_dict', 'func_doc', 'func_globals',

All those quoted things that the dir() function spit out are in the module.

>>> dir(re.match.__doc__)

'Try to apply the pattern at the start of the string, returning\n
a match object, or None if no match was found.'

You can find all sorts of stuff like this. It's somewhat cryptic at first,
but with the above documentation in hand, you can figure it out
eventually. =)

bhaaluu at gmail dot com

More information about the Tutor mailing list