Search code examples
pythonregexpython-re

How to remove a special character based on negative pattern matching using regular expression


I have a sample string of something like hello \+ \\\world \+ \\\\ this \234 \ is \Pattern\ and I want it to be something like hello + \world + this 234 is \Pattern

One way to do is to run a loop for every character in the string and if it's a \ and next character is NOT a word, then replace it with a space. Simple but inefficient code. There must be another way to do it using regular expression.

I can find all the \alphabet using r'\\\w+' and any single \ followed by space as \\\s+ but these won't take \\\ \( \+ into consideration. How can this be done?


Solution

  • Maybe use:

    \\(?![A-Za-z])\s*
    

    And replace with empty string as per this online demo

    • \\ - A backslash (escaped);
    • (?![A-Za-z]) - Negative lookahead to assert not being followed by alphachar;
    • \s* - 0+ (Greedy) whitespace-chars.