I'm trying to use regex to find a pattern in a string and replace it with another pattern
I want to change the _
to -
but only either side of numbers in between two text sections
Specifically, I have a pattern like:
original = "first_part_1234_this_is_the_second_part"
and would like:
desired = "first_part-1234-this_is_the_second_part"
i.e. keeping _
in the first and second parts of the string.
I can use ()[_]\d*[_]()
to get the numbers in the middle of the string and
desired = re.sub(r"()[_](\d*)[_]()", r"-\2-", original)
gives me 'first_part-this_is_the_second_part'
, i.e. identifies the numbers and replaces the first _
with -
.
I've not used regex before, and can't work out how to replace the pattern with another pattern.