首页 > 解决方案 > How can I replace the first occurrence of a character in every word?

问题描述

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?

标签: pythonregex

解决方案


我将对以下模式进行正则表达式替换:

@(@*)

然后只需替换第一个捕获组,它都是连续的@符号,减一。

这应该捕获@每个单词开头出现的每个单词,无论是字符串开头、中间还是结尾的单词。

inp = "hello @jon i am @@here or @@@there and want some@thing in '@here"
out = re.sub(r"@(@*)", '\\1', inp)
print(out)

这打印:

hello jon i am @here or @@there and want something in 'here

推荐阅读