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?
I would do a regex replacement on the following pattern:
@(@*)
And then just replace with the first capture group, which is all continous @ symbols, minus one.
This should capture every @
occurring at the start of each word, be that word at the beginning, middle, or end of the string.
inp = "hello @jon i am @@here or @@@there and want some@thing in '@here"
out = re.sub(r"@(@*)", '\\1', inp)
print(out)
This prints:
hello jon i am @here or @@there and want something in 'here
How about using replace('@', '', 1)
in a generator expression?
string = 'hello @jon i am @@here or @@@there and want some@thing in "@here"'
result = ' '.join(s.replace('@', '', 1) for s in string.split(' '))
# output: hello jon i am @here or @@there and want something in "here"
The int value of 1
is the optional count
argument.
str.replace(old, new[, count])
Return a copy of the string with all occurrences of substring old replaced by new . If the optional argument count is given, only the first count occurrences are replaced.
You can use re.sub
like this:
import re
s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
s = re.sub('@(\w)', r'\1', s)
print(s)
That will result in:
"hello jon i am @here or @@there and want something in 'here"
And here is a proof of concept:
>>> import re
>>> s = "hello @jon i am @@here or @@@there and want some@thing in '@here"
>>> re.sub('@(\w)', r'\1', s)
"hello jon i am @here or @@there and want something in 'here"
>>>
Was pondering for cases what if only the last char is @
and you don't want to remove it, or you have specific allowed starting chars, came up with this:
>>> ' '.join([s_.replace('@', '', 1) if s_[0] in ["'", "@"] else s_ for s_ in s.split()])
"hello jon i am @here or @@there and want some@thing in 'here"
Or, suppose you want to replace @
only if it is in first n characters
>>> ' '.join([s_.replace('@', '', 1) if s_.find('@') in range(2) else s_ for s_ in s.split()])
"hello jon i am @here or @@there and want some@thing in 'here"
# Python3 program to remove the @ from String
def ExceptAtTheRate(string):
# Split the String based on the space
arrOfStr = string.split()
# String to store the resultant String
res = ""
# Traverse the words and
# remove the first @ From every word.
for a in arrOfStr:
if(a[0]=='@'):
res += a[1:len(a)] + " "
else:
res += a[0:len(a)] + " "
return res
# Driver code
string = "hello @jon i am @@here or @@@there and want some@thing in '@here"
print(ExceptAtTheRate(string))
Output:
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.