You can go a long way just by adding the word boundary (\b
) assertion, but I'd like to suggest that you (1) use re.VERBOSE
to write a regexp someone can understand later; (2) compile the regexp to reduce clutter at the call site; and, (3) tighten some of the matching criteria. Like so:
def conv(m):
return chr(int(m.group(1)))
pat = re.compile(r"""[+\s]* # optional whitespace or +
\b # word boundary
n?char # NCHAR or CHAR
\( # left paren
([\d\s]+) # digits or spaces - group 1
\) # right paren
[+\s]* # optional whitespace or +
""", re.VERBOSE | re.IGNORECASE)
print pat.sub(conv, data)
Note that I changed your str
to data
: str
is the name of a heavily used builtin function, and it's a Really Bad Idea to create a variable with the same name.