正则表达式以匹配linux路径

2024-03-28 23:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试过stackoverflow中的许多正则表达式,比如: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.
"""

我想使用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.
"""

Tags: andpathrestringbinmoreextensionsome