tags:

views:

77

answers:

3

This should be easy, but I've managed to stump 2 people so far at work & I've been at it for over 3 hours now, so here goes.

I need to replace a+ with aplus (along with a few other cases) with the Python re module. eg. "I passed my a+ exam." needs to become "I passed my aplus exam."

Just using \ba+ works fine most of the time, but fails in the case of a+b, so I can't use it, it needs to match a+ as a distinct word. I've tried \ba+\b but that fails because I assume the + is a word boundary.

I've also tried \ba+\W which does work, but is greedy and eats up the space (or any other non-alpha char that would be there).

Any suggestions please?

+8  A: 

Turn that \W into an assertion.

\ba\+(?=\W)

or, better,

\ba\+(?!\w)

since the negative assertion allows matching the a+ at end of string too.

KennyTM
Thanks, works great
dochead
+1  A: 
>>> re.sub(r'\ba\+\s', 'aplus ', 'I passed my a+ exam.')
'I passed my aplus exam.'
>>> re.sub(r'\ba\+\s', 'aplus ', 'a+b')
'a+b'
SilentGhost
A: 

You need to escape the + as it has a special meaning in regexp (one or many a's). search a\+ instead of a+

baloo