Asked By: Anonymous
I have a set of selected characters, and I want to replace any char that is not in my selected characters with a whitespace.
the problem is regex catches everything between my defined chars as a null group and adds an space between all chars.
this is my code:
import re text = "ABCDDDAB" text = re.sub('[^ABC]*',' ', text) # replace all characters that are not in ABC by a whitespace text = re.sub(' +',' ', text) # replace all trailing whitespaces with a single whitespace print(text)
I expect the result to be
"ABC AB" but instead I get
"A B C A B".
I tried this in online regex tools and I know the problem is that regex is matching null groups between all the chars I defined and replacing them with whitespace.
I’ll attach a picture of online regex match, the pink lines are my problem, I don’t want them to match, but they somehow do match as
group 0 : null .
at the time of asking the question, I had no idea the issue is using
* instead of
+. I thought I somehow defined some null groups without knowing, and someone might make the same mistake. so marking this as duplicate is not really accurate.
Answered By: Anonymous
You should try this expression :
because you want the "bad" letters to appears at least once
Output is :