[英]Regular expression to match linux path
我嘗試了許多在stackoverflow中找到的正則表達式,例如:
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.
"""
我想使用 python re.sub
用單詞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.
"""
您可以使用
mystring = re.sub(r'[/\\](?:(?!\.\s+)\S)+(\.)?', r'PATH\1', mystring)
print(mystring)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.