Reputation: 972
I have tried many regular expressions found in stackoverflow like: This
import re
mystring = """
some string /bin/path and also /opt/something/bin/.
Some other string /home/user/file.extension.
Some more string \.out and \.cpp and \.extension.
All these are paths and needs to be captured.
"""
I wanted to use python re.sub
to replace all paths with word PATH
.
output = """
some string PATH and also PATH.
Some other string PATH.
Some more string PATH and PATH.
All these are paths and needs to be captured.
"""
Upvotes: 0
Views: 1741
Reputation: 43169
You may use
mystring = re.sub(r'[/\\](?:(?!\.\s+)\S)+(\.)?', r'PATH\1', mystring)
print(mystring)
Upvotes: 6