Регулярное выражение для соответствия linux пути - PullRequest
0 голосов
/ 08 апреля 2020

Я пробовал много регулярных выражений, найденных в 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.
"""

1 Ответ

4 голосов
/ 08 апреля 2020

Вы можете использовать

mystring = re.sub(r'[/\\](?:(?!\.\s+)\S)+(\.)?', r'PATH\1', mystring)
print(mystring)

См. демо на regex101.com .

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...