簡體   English   中英

正則表達式匹配 linux 路徑

[英]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)

請參閱regex101.com 上的演示

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM