MoinMoin WikiName and python regexes

deelan ggg at zzz.it
Wed Jun 8 12:31:08 EDT 2005


Ara.T.Howard wrote:
(...)
> and i'm not that familiar with python syntax.  to me this looks like a map
> used to bind variables into the regex - or is it binding into a string then
> compiling that string into a regex - regexs don't seem to be literal 
> objects
> in pythong AFAIK...  i'm thinking i need something like
> 
>   word_rule = 
> ur'(?:(?<![%(l)s])|^)%(parent)s(?:%(subpages)s(?:[%(u)s]+[%(l)s]+){2,})+(?![%(u)s%(l)s]+)' 
> % {
>                                                                       ^
>                                                                       ^
>                                                                       ^
>       'u': config.chars_upper,
>       'l': config.chars_lower,
>       'subpages': config.allow_subpages and (wikiutil.CHILD_PREFIX + 
> '?') or '',
>       'parent': config.allow_subpages and (ur'(?:%s)?' % 
> re.escape(PARENT_PREFIX)) or '',
>   }
> 
> and this seems to work - but i'm wondering what the 's' in '%(u)s' implies?
> obviously the u is the char range (unicode?)...  but what's the 's'?

an example may help here:

 >>> a = 123
 >>> '%04d' % a
'0123'
 >>> '%f' % a
'123.000000'
 >>> '%s' % a
'123'

that "s" tells python to convert the number as string. the form %(key)s 
tells python to lookup a dictionary "key" and format the found  value 
into a string:

 >>> d = {'key': 123}
 >>> '%(key)s' % d
'123'

so in your code there's some keys named 'u', 'l', 'subpages', etc. and 
their values are substitued into that big RE, replacing the 
corresponding key names.

HTH.

-- 
deelan <http://www.deelan.com/>





More information about the Python-list mailing list