Search code examples
regexnotepad++

How do I separate a Filepath containing backslashes by replacing only the rootlevel backslash with a tab?


I have a list of Filepaths, each row includes the filesize and creationdate.

The Filepaths are separated by backslashes, some files in the root folder, others in sub-folders or even in sub-sub-folders.

I am trying to use RegEx in Notepad++ to achieve the desired results.

I merely wish to replace the backslash immediately after the root folder with a tab character.

Example:

Current:

   289399    13/12/2021    ALLEN Alan - 12.05.2014 - A\File in Root Folder.JPG
    67795    21/12/2021    JONES Jess - 14.09.2010 - B\SUB\File in Level 1 Sub Folder.TXT
   110842    27/05/2020    SMITH Alex - 08.01.2006 - C\SUB\SUB\File in Level 2 Sub-Sub Folder.PDF

Desired:

   289399    13/12/2021    ALLEN Alan - 12.05.2014 - A <Tab> File in Root Folder.JPG
    67795    21/12/2021    JONES Jess - 14.09.2010 - B <Tab> SUB\File in Level 1 Sub Folder.TXT
   110842    27/05/2020    SMITH Alex - 08.01.2006 - C <Tab> SUB\SUB\File in Level 2 Sub-Sub Folder.PDF

Tab separation required at Root Level ONLY

Various methods I have tried using RegEx in Notepad++ fail to achieve the desired result. The tab character almost always replaces the very last backslash character, not the one immediately after the root foldername.

Find:

^(.*\t.*\t.*)\\((.*)$|(.*\\.*)$|(.*\\.*\\.*)$)$

RegEx:

\1\t\2

Result:

   289399    13/12/2021    ALLEN Alan - 12.05.2014 - A <Tab> File in Root Folder.JPG
    67795    21/12/2021    JONES Jess - 14.09.2010 - B\SUB <Tab> File in Level 1 Sub Folder.TXT
   110842    27/05/2020    SMITH Alex - 08.01.2006 - C\SUB\SUB <Tab> File in Level 2 Sub-Sub Folder.PDF

The above RegEx (and variations of this) result in only the final backslash being replaced with a Tab.

I need to replace the first backslash after the Root Foldername only to be replaced, leaving the rest intact.

Note: Some rows will not have a letter immediately before the first backslash, e.g.

   289399    13/12/2021    ALLEN Alan - 12.05.2014\File in Root Folder.JPG

Please can somebody advise of a simple solution for this?


Solution

    • Ctrl+H
    • Find what: ^[^\\]+\K\\
    • Replace with: \t
    • TICK Wrap around
    • SELECT Regular expression
    • Replace all

    Explanation:

    ^           # beginning of line
    [^\\]+      # 1 or more any character that is not a backslash
    \K          # forget all we have seen until this position
    \\          # a backslash
    

    Replacement:

    \t          # a tabulation
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here