re.compile versus r''
Terrence Brannon
metaperl at gmail.com
Thu Aug 28 15:23:10 EDT 2008
Hello, I'm using a tool (PLY) which apparently expects the tokens to
be created using r''
But because one token is a rather complex regular expression, I want
to create the regular expression programmatically.
How can I generate a string and then create something of the same type
that the r'' function does?
Concretely, in the program below, consonant is not the same type as
t_NAME, but I assume that it needs to be for PLY to use it for
tokenizing:
import re
t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
guttural = 'kh?|gh?|\"n'
palatal = '(?:chh?|jh?|\~n)'
cerebral = '\.(?:th?|dh?|n)'
dental = '(?:th?|dh?|n)'
semivowel = '[yrlv]'
sibilant = '[\"\.]?s'
aspirant = 'h'
consonant = re.compile('|'.join([guttural , palatal , cerebral ,
dental , semivowel , sibilant , aspirant]))
print consonant
print t_NAME
More information about the Python-list
mailing list