How can I replace the first occurrence of a character in every word?
Say I have this string:
hello @jon i am @@here or @@@there and want some@thing in '@here"
# ^ ^^ ^^^ ^ ^
And I want to remove the first @ on every word, so that I end up having a final string like this:
hello jon i am @here or @@there and want something in 'here
# ^ ^ ^^ ^ ^
Just for clarification, "@" characters always appear together in every word, but can be in the beginning of the word or between other characters.
I managed to remove the "@" character if it occurs just once by using a variation of the regex I found in Delete substring when it occurs once, but not when twice in a row in python, which uses a negative lookahead and negative lookbehind:
@(?!@)(?<!@@)
See the output:
>>> s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
>>> re.sub(r'@(?!@)(?<!@@)', '', s)
"hello jon i am @@here or @@@there and want something in 'here"
So the next step is to replace the "@" when it occurs more than once. This is easy by doing s.replace('@@', '@') to remove the "@" from wherever it occurs again.
However, I wonder: is there a way to do this replacement in one shot?
