Given a string of the form <digit>-<non-digit>
or <non-digit>-<digit>
, I need to remove the hyphen (in Python). I.e. 2-f
becomes 2f
, f-2
becomes f2
.
So far I have (?:\d-\D)|(?:\D-\d)
, which finds the patterns but I can't figure out a way to replace the hyphen with blank. In particular:
- if I
sub
the regex above, it will replace the surrounding characters (because they are the ones matched); - I can do
(?:(\d)-(\D))|(?:(\D)-(\d))
to expressly capture the characters and thensub
with\1\2
will correctly process2-f
, turning it to2f
... but! it will failf-2
of course because those characters are in the 3rd and 4th groups, so we'll need to sub with\3\4
. Tried to give names to the group failed because all names need to be unique.
I know I can just run it through 2 sub
statements, but is there a more elegant solution? I know regex is super-powerful if you know what you're doing... Thank you!