Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

187
Views
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?

over 4 years ago · Santiago Trujillo
6 answers
Answer question

0

DEMO

(?<!@)@

You can try this. See demo.

over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report

0

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
over 4 years ago · Santiago Trujillo Report

0

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"
over 4 years ago · Santiago Trujillo Report

0

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"
>>> 
over 4 years ago · Santiago Trujillo Report

0

# 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:

enter image description here

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!