string replace() function perfectly solves this problem:
string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.
>>> u'longlongTESTstringTEST'.replace('TEST', '?', 1)
u'longlong?stringTEST'
Answer from virhilo on Stack Overflowstring replace() function perfectly solves this problem:
string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.
>>> u'longlongTESTstringTEST'.replace('TEST', '?', 1)
u'longlong?stringTEST'
Use re.sub directly, this allows you to specify a count:
regex.sub('', url, 1)
(Note that the order of arguments is replacement, original not the opposite, as might be suspected.)
python - Replace first occurrence only of a string? - Stack Overflow
regex - How to replace the first occurrence of a regular expression in Python? - Stack Overflow
python - How can I replace the first occurrence of a character in every word? - Stack Overflow
Pattern to match first occurrence in line?
text = text.replace("very", "not very", 1)
>>> help(str.replace)
Help on method_descriptor:
replace(...)
S.replace (old, new[, count]) -> string
Return a copy of string S with all occurrences of substring
old replaced by new. If the optional argument count is
given, only the first count occurrences are replaced.
text = text.replace("very", "not very", 1)
The third parameter is the maximum number of occurrences that you want to replace.
From the documentation for Python:
string.replace(s, old, new[, maxreplace])
Return a copy of string s with all occurrences of substring old replaced by new. If the optional argument maxreplace is given, the first maxreplace occurrences are replaced.
re.sub() has a count parameter that indicates how many substitutions to perform. You can just set that to 1:
>>> s = "foo foo foofoo foo"
>>> re.sub("foo", "bar", s, 1)
'bar foo foofoo foo'
>>> s = "baz baz foo baz foo baz"
>>> re.sub("foo", "bar", s, 1)
'baz baz bar baz foo baz'
Edit: And a version with a compiled SRE object:
>>> s = "baz baz foo baz foo baz"
>>> r = re.compile("foo")
>>> r.sub("bar", s, 1)
'baz baz bar baz foo baz'
Specify the count argument in re.sub(pattern, repl, string[, count, flags])
The optional argument count is the maximum number of pattern occurrences to be replaced; count must be a non-negative integer. If omitted or zero, all occurrences will be replaced.
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.