[Tutor] Help with Python Regular Expressions
Greg Chapman
greg@gregmchapman.info
Thu Jun 26 12:24:01 2003
>def makeIfDefName(fileName):
> import re
> p = re.compile('([A-Z])')
> newFileName = p.sub(r'_\1', fileName)
> return newFileName.upper()
>
>When given "FooBar" this returns "_FOO_BAR". I could do it without
regular
>expressions, I suppose, but I'd like to learn RE's too.
Try your regular expression like this: p = re.compile(r'\B([A-Z])')
The '\B' specifies that the match not occur on a word boundary, ie at
the beginning of the word. This works for me, although I've only
subjected it to limited testing.
HTH,
greg