user3059024 user3059024 - 3 months ago 7
Python Question

Replacing a conditional with the negative (i.e. using not) causes error with pythons re.sub method

I'm trying to replace all characters in a string that windows doesn't like in a file path (such as

%$£^
) with an empty string, using
re.sub
. When I use this:

import string
import re
name= '*example -name_with^additional£$characters'
for j in name:
if j in string.ascii_letters+string.digits+'_-':
name=re.sub(j,'',name)


The regular expression works and replaces all the characters with an empty string. However, since I want to replace characters NOT in
string.ascii_letters+string.digits+'_-'
i need to use
not
instead...

import string
import re
name= '*example -name_with^additional£$characters'
for j in name:
if j not in string.ascii_letters+string.digits+'_-':
name=re.sub(j,'',name)


However, this gives me an error:

File "C:\Anaconda2\lib\re.py", line 155, in sub
return _compile(pattern, flags).sub(repl, string, count)

File "C:\Anaconda2\lib\re.py", line 251, in _compile
raise error, v # invalid expression

error: unexpected end of regular expression


Does anybody know what is going on? Thanks

Answer

The issue arises because you are trying to re.sub with * pattern, and it is an invalid regex pattern since you cannot quantify nothing (empty location).

Use a simple replace:

import string
name= '*example -name_with^additional£$characters'
for j in name:
    if j  not in string.ascii_letters+string.digits+'_-':
        name=name.replace(j,'') # <- here
print(name)

See Python demo