R – Place character right next to regex group reference

regex

How can I place characters directly next to a group reference in regex? For example, if I am replacing the expression "0([0-9])" with "\1 aaa" it will show the number with a space and then "aaa" next to it. I want to place "aaa" directly next to the number without a space in between them.

EDIT: Sorry, I forgot the issue in my example. I am trying to place a number directly next to the group reference, which does not work. I am using the Python re module.

Best Answer

Use \g instead of \n:

 re.sub(r'0([0-9])',r'\g<1>4',"foo02")

For more information: http://docs.python.org/library/re.html#re.sub