Search code examples
pythonpython-3.xregexregex-group

adding space before and after a comma and dash in python using regex


I've already looked at so many topics on this and maybe I am not understanding something, but in a phrase like this:

"hello-goodbye C-GOOD" or "100.89 D-FARM" I want to add a space before and after characters like ".,-" but I don't want those spaces in C-GOOD or D-FARM"

This is the final result I want: "hello - goodbye C-GOOD" "100 . 89 D-FARM" but no matter what I try I either get spaces on everything or no spaces on any of the dashes:

I either get: "hello - goodbye C - GOOD" "100 . 89 D - FARM" or "hello-goodbye C-GOOD" "100 . 89 D-FARM" Here is what I have tried:

text= re.sub(r'([.,!?()-]+)^(?<!C)', r' \1 ', text)
text= re.sub(r'([.,!?()-]+)^(?<!C-)', r' \1 ', text)
text= re.sub(r'([.,!?()-]+)(?<!C-GOOD)', r' \1 ', text)
text= re.sub(r'([.,!?()-]+)(?!C-GOOD)', r' \1 ', text)

If anyone can help or knows what I am doing wrong that would be wonderful. Thank-you.


Solution

  • Mismatches .,- join uppercase letters.

    import re
    
    s = "hello-goodbye C-GOOD 100.89 D-FARM"
    
    print(re.sub("(?<![A-Z])([.,-])(?![A-Z]+)", r" \g<1> ", s))
    # hello - goodbye C-GOOD 100 . 89 D-FARM