Looking for a regex that allow to remove all occurences for an character(alphabet) and let only the first occurence, for example: input string = AAAAAABBBBBBBCDEEEEAAAAAAFFFBBB output should be string = ABCDEF thanks for your help
CodePudding user response:
You can use regex but you can also use sets because set removes duplicates
First Solution:
regex = re.compile(r'\b(' remove r')\b', flags=re.IGNORECASE)
Second Solution:
string = "AAAAAABBBBBBBCDEEEEAAAAAAFFFBBB"
new_set = sorted(set(string))
print(new_set)
CodePudding user response:
You could try:
([A-Z])(?<=\1.*?\1)
See an online demo
([A-Z])
- 1st Capture group holding a single alpha-char;(?<=\1.*?\1)
- Positive lookbehind to assert position is at least preceding by a copy of itself prior to itself.
Substitute matches with empty string.
Note: Not all applications will support this type of zero-width lookbehind.