Search code examples
regexnotepad++notepad

Notepad++ REGEX find and replace second-last slash


I have a big textfile with multiple paths like the following examples. The paths are always different. I'm looking for a regex (find and replace) in Notepad++ to replace the second-last "/" with "/;".

Example:

/testNAS/questions/ask/test/example/picture.png

After Replacing:

/testNAS/questions/ask/test/;example/picture.png

I tried with the regular expression /(?=[^/]*$) but this only marks the last slash.

Can anyone help me please?


Solution

  • You can use

    /(?=[^/\v]*/[^/\v]*$)
    

    Replace with $0;. See the regex demo.

    Details

    • / - a slash
    • (?=[^/\v]*/[^/\v]*$) - a positive lookahead that requires zero or more chars other than / and vertical whitespace, / and again zero or more chars other than / and vertical whitespace at the end of a line.

    The $0; replacement pattern inserts the whole match value ($0) and then a ; char in place of a match.