Reputation: 5705
I have the following string : "n && && && && && &&n"
and I want to substitiute &&
with and
but only those &&
s which have a space before and after them.
I am using the below code:
import re
str = input()
change = re.sub(r' (&&) ', ' and ', str)
print (change)
But the output is coming as: n and && and && and &&n
The correct output should be: n and and and and and &&n
Can someone please tell me where I am going wrong?
Upvotes: 0
Views: 52
Reputation: 49320
In your expression, you are expecting the spaces to be counted twice, but a space after an &&
will make it unavailable to be counted as a space before the next &&
. You are looking for "lookarounds":
>>> import re
>>> s = 'n && && && && && &&n'
>>> change = re.sub(r'(?<= )(&&)(?= )', 'and', s)
>>> print (change)
n and and and and and &&n
These "look" for the given matches without actually consuming them. You're actually only interested in the and
s; they just have to be surrounded by the spaces.
Also, don't name your variable str
or you make it difficult to access the built-in function str()
.
Upvotes: 1
Reputation: 12255
Your regular expression will not look at the ending space again after doing one substitution, so the next character checked will be "&& ...". Instead "lookahead" for a space instead of substituting for it:
change = re.sub(r' (&&)(?= )',' and',str)
Upvotes: 0
Reputation: 11831
The trouble is that the 'n && ', at the beginning, is picked up by the first substitution match. Once that space after the && is removed (albeit, replaced by a space in the ' and ',) it can't be used in another match.
The good news is that you'll never end up with consecutive &&s after your sub pass and - for the simplest solution - you can just run sub twice:
In [7]: input_str = 'n && && && && && &&n'
In [8]: first_sub = re.sub(r' (&&) ', ' and ', input_str)
In [9]: second_sub = re.sub(r' (&&) ', ' and ', first_sub)
In [10]: print second_sub
n and and and and and &&n
Upvotes: 0