I'm trying to remove spaces between numbers immediately following a string (po box in this case).
I can acheive this using re.match and some replace logic, but it sure would be prettier if i could do this with a conditional re.sub.
How can i integrate a capture group into a regex sub?
I've tried many different versions of the code immediately below, but to no avail:
my_string = "po box 12 34 5 heisenburg 902 rr 15"
re.sub(r'(?:po box)([0-9 ]+)', r'', my_string)
Expected results:
po box 12 59 76 => po box 125976
po box 56 56 barry 56 87 => po box 5656 barry 56 87
barry box 56 87 => barry box 56 87
I've put this together that has the desired effect, but is not ideal.
my_string = "po box 12 34 5 heisenburg 902 rr 15"
match = re.match(r'po box([0-9 ]+)', my_string)
if match:
# remove spaces between numbers
spaceless_numbers = match.group(1).replace(' ', '')
# get original string positions
start = match.span(1)[0]
end = match.span(1)[1]
# get start and end portions of the original string
first_part = my_string[:start]
second_part = my_string[end:]
# concatenate start + spaces removed section + end
print('{} {} {}'.format(first_part, spaceless_numbers, second_part).strip())