Search code examples
pythonsubstringpython-re

Python re expression - remove # followed by numbers up until a comma


I have an string:

5956 Executive Dr #101, Fitchburg, WI 53719

I want it to be:

5956 Executive Dr, Fitchburg, WI 53719

I've tried:

string2 = re.sub("(\\.*)\\s+#.*", "\\1", string1)

But that just gets me:

5956 Executive Dr

How can I do this in Python? I find re expressions so confusing. Ideally I'd like to do the same thing with Apt, Unit, etc - replace any instance like "Apt 3", "Unit 3", "Suite 4" with "".


Solution

  • I think your approach is not the best here, you can use a simpler expression:

    string2 = re.sub("\s#\d+", "", string1)
    
    • \s - whitespace
    • # - literal octothorpe
    • \d+ - at least one digit

    To also remove the other parts you can use "(Apt|Unit|Suite)\s\d+" or together "\s#\d+|(Apt|Unit|Suite)\s\d+"