Search code examples
regexpython-3.xregex-lookaroundsregex-group

Remove spaces in words including ampersand with Regex


In python3, using the re.sub(pattern, replacement, string), I have been able to remove spaces around ampersand. For instance, the following phrases should be modified as follows:

"A & D ALTERNATIVE SOLUTIONS" to "AD ALTERNATIVE SOLUTIONS"

"WESTMOUNT PLUMBING & HEATING" to "WESTMOUNT PLUMBING HEATING"

"A PANORAMIC VIEW B & B" to "A PANORAMIC VIEW BB"

I have been able to fix the first two patterns, but I am having problems with the third, I tried the following:

import re
#str="A & D ALTERNATIVE SOLUTIONS LTD."
#str="WESTMOUNT PLUMBING & HEATING LTD."
str= "A PANORAMIC VIEW B & B"


str = re.sub('(?<=\\&)\s','',str)
str = re.sub('(?<=^[A-Z])\s','',str)
str = re.sub('[^\w\s]','',str)

print(str)

How could I get all the patterns?


Solution

  • you could do the following:

    strlist = ["A & D ALTERNATIVE SOLUTIONS LTD.","WESTMOUNT PLUMBING & HEATING LTD.", "A PANORAMIC VIEW B & B"] 
    pattern = r'(\w{2}\s)?\s?& (\w{2})?'
    
     [re.sub(pattern,r'\1\2',str_) for str_ in strlist]
    Out[120]: 
    ['AD ALTERNATIVE SOLUTIONS LTD.',
     'WESTMOUNT PLUMBING HEATING LTD.',
     'A PANORAMIC VIEW BB']