how to remove file/folder path from string using regex

Raj De Inno

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)
The fourth bird

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 group

See a Regex demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to remove folder names from a file path using regex in javascript

How to remove certain string from the original string using regex in Java

How to remove specific string from string using regex

How to create a FileFolder in Windows?

How to remove unmatched part from string using JS/jQUery regex

How to remove url starting with www.*** from a string using regex?

How to remove punctuation from a string with exceptions using regex in bash

How to remove html tags from an Html string using RegEx?

How do you remove .00 from a string using Regex function?

Find string from path using regex

Remove tag from string variable using Regex

Remove data from String using Regex

Using regEx to remove digits from string

remove empty quotes from string using regex

Remove numbers from beginning of string using regex

Remove leading zeros from a string using regex

Remove digits from the string if they are concatenated using Regex

how to remove folder path from a string in python

How to remove a path from system path(`$PATH`) using terminal commands?

How to remove the backslash in string using regex in Java?

How to remove duplicate characters in a string using regex?

Using regex how to remove a date,time in a string?

How to search and remove a string using regex?

How to remove string "[xyz]" from filename with Regex?

How do i remove '/' from the path without using string manipulation and using pathlib

How to extract filename from path using regex

Using regex to remove a string

Regex remove 'by' from a string

How to remove # from hashtag using Python RegEx