0

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.

ceharep
  • 419
  • 1
  • 5
  • 12
  • *Capture* the digit matching pattern and then *backreference* the value using `r"\1"`. – Wiktor Stribiżew Feb 28 '20 at 09:33
  • @AlbinPaul the OP doesn't want to replace ALL occurrences of `_`, and it not clear how many `_`s there are in the first part (or even if their number is fixed). – Błotosmętek Feb 28 '20 at 09:50
  • 1
    looked into [backreferences](https://www.regular-expressions.info/backref.html) and this: `desired = re.sub(r"()[_]\d*[_]()", r"-\2-", original)` seems to work. – ceharep Feb 28 '20 at 10:10

0 Answers0