Reputation: 581
I have a string something like this:
opt/custom/building/BuildingInput/address/BuildingUnderwritingInput/Name
I need to catch all the words having 'Input' and delete them from the path. So my final string will be:
opt/custom/building/address/Name
I tried something like this but it didnt work
x = "opt/custom/building/BuildingInput/address/BuildingUnderwritingInput/Name"
re.sub(r'Input/', r'/' , x.rstrip())
And it gave me
opt/custom/building/Building/address/BuildingUnderwriting/Name
The "Building" of "BuildingInput" and "BuildingUnderwriting" of "BuildingUnderwritingInput" are retained here. I want the whole word 'BuildingInput" and "BuildingUnderwritingInput" to be omitted. Any help? Or if anyone can tell me how I can backtrace from occurrence of "Input" to the first occurrence of "/" so that I can match the whole word "BuildingInput" and "BuildingUnderwritingInput"
Upvotes: 0
Views: 280
Reputation: 4981
Use this regex to remove all words ending with Input within slashes (/):
(/)[^/]+Input(?=/)
For your case:
x = "opt/custom/building/BuildingInput/address/BuildingUnderwritingInput/Name"
re.sub(r'(/)[^/]+Input(?=/)', r'' , x.rstrip())
Upvotes: 1
Reputation: 1112
Remove 0 or more chars that are not a slash ([^/]*
) till after the point that Input
followed by a slash appears:
import re
x = "opt/custom/building/BuildingInput/address/BuildingUnderwritingInput/Name"
print(re.sub(r'[^/]*Input/', r'' , x.rstrip()))
If it is possible that the last element of the path also contains an Input
word (without a trailing slash) you can use this instead:
x = "address/BuildingUnderwritingInput"
print(re.sub(r'[^/]*Input(/|$)', r'' , x.rstrip()))
Here either /
or the end of the string ($
) match after Input
. However this leaves one slash if the last word is matched. If this is a problem you can remove it seperately:
x = "address/BuildingUnderwritingInput"
x = re.sub(r'[^/]*Input(/|$)', r'' , x.rstrip())
print(re.sub(r'/$', r'' , x))
Upvotes: 0
Reputation: 3570
Currently you are only searching and replacing Input/
, you have to search for the whole word, for example by using this regex:
re.sub(r'/\w*Input/', r'/' , x.rstrip())
Upvotes: 0