Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace dash between characters with space using regex

Tags:

python

regex

I want to replace dashes which appear between letters with a space using regex. For example to replace ab-cd with ab cd

The following matches the character-character sequence, however also replaces the characters [i.e. ab-cd results in a d, rather than ab cd as i desire]

 new_term = re.sub(r"[A-z]\-[A-z]", " ", original_term)

How i adapt the above to only replace the - part?

like image 228
kyrenia Avatar asked Oct 13 '15 21:10

kyrenia


2 Answers

You need to capture the characters before and after the - to a group and use them for replacement, i.e.:

import re
subject = "ab-cd"
subject = re.sub(r"([a-z])\-([a-z])", r"\1 \2", subject , 0, re.IGNORECASE)
print subject
#ab cd

DEMO

http://ideone.com/LAYQWT


REGEX EXPLANATION

([A-z])\-([A-z])

Match the regex below and capture its match into backreference number 1 «([A-z])»
   Match a single character in the range between “A” and “z” «[A-z]»
Match the character “-” literally «\-»
Match the regex below and capture its match into backreference number 2 «([A-z])»
   Match a single character in the range between “A” and “z” «[A-z]»

\1 \2

Insert the text that was last matched by capturing group number 1 «\1»
Insert the character “ ” literally « »
Insert the text that was last matched by capturing group number 2 «\2»
like image 95
Pedro Lobito Avatar answered Nov 03 '22 01:11

Pedro Lobito


Use references to capturing groups:

>>> original_term = 'ab-cd'
>>> re.sub(r"([A-z])\-([A-z])", r"\1 \2", original_term)
'ab cd'

This assumes, of course, that you can't just do original_term.replace('-', ' ') for whatever reason. Perhaps your text uses hyphens where it should use en dashes or something.

like image 41
TigerhawkT3 Avatar answered Nov 03 '22 01:11

TigerhawkT3