1

I'm attempting to modify strings using the .sub() function from the re module. More specifically, I'm trying to use a group backref but the function doesn't seem to register the function. For example:

> In [49]: s = ' STORE # 123  123 '
> In [50]: print re.sub('([0-9]+) +(\1)','(\1)',s)  
 STORE # 123  123

I want it to print "STORE # 123" but it seems like the first arg of .sub() isn't registering so it just spits out the initial string unmodified. I've even checked the documentation (https://docs.python.org/2/library/re.html#re.sub) and still can't figure out what I'm doing wrong. I'm running Python 2.7 by the way.

Thanks for the help!

0

2 Answers 2

2

You should use:

>>> print re.sub(r'([0-9]+) +\1', r'(\1)', ' STORE # 123  123 ')
STORE # (123) 

You use r'...' in order not to have to escape backslashes.

Sign up to request clarification or add additional context in comments.

Comments

0

You can preserve what you want if you remove it from the match result. To do it you only need to enclose the backreference in a lookahead ((?=...) followed with ):

print re.sub(r'([0-9]+) +(?=\1)','',s)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.