I want to remove any file/folder path from the given string. I use regex to identify the paths and replace it with empty string.
This is my example string.
\\172.44.43.90\c\Logs\AccessService\2021\June The public disk is: \\diskA\FolderB\SubFolderC\Files file path /log/file.txt some lines /log/var/file2.txt
My require output is,
\\172.44.43.90\c\Logs\AccessService\2021\June
\\diskA\FolderB\SubFolderC\Files
/log/file.txt
/log/var/file2.txt
I should remove the above strings and should get my final output, The public disk is: file path some lines
I tried below regex. but it is only identifying files. I need to identify any dynamic files/folders. Note: forward or backward slash may come in the start of the filepath.
My code :
import re
re.sub(r"(\/.*?\.[\w:]+)", "", input)
One option could be starting the match (instead of using re.sub) with either \\
or /
(?<!\S)(?:\\\\\S+|\/.*?\.[\w:]+)
(?<!\S)
Assert a whitspace boundary to the left(?:
Non capture group
\\\\\S+
match \\
and 1+ non whitespace chars (no spaces)|
Or\/.*?
Match /
then as least as possible chars (including spaces)\.[\w:]+
Match .
and 1+ occurrences of either a word char or :
)
Close non capture groupSee a Regex demo
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments