syslog
syslog

Reputation: 53

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?

Upvotes: 5

Views: 1424

Answers (4)

Haji Rahmatullah
Haji Rahmatullah

Reputation: 430

Try this, if you want to replace the fifth number slash, in your case the second-last from right to left..
Find what: ^(.*?\K/){5}
Replace with: /;

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133518

With your shown samples only, you could try following regex.

find what: ^(/[^/]*/[^/]*/[^/]*/[^/]*/)(.*)$

Replace with: $1;$2

Online Demo for above regex

Explanation: Adding detailed explanation for above regex.

^(                           ##Matching from starting of value in 1st capturing group.
  /[^/]*/[^/]*/[^/]*/[^/]*/  ##Matching / followed by till next occurrence of / doing this 4 times total in here.
)                            ##Closing 1st capturing group here.
(.*)$                        ##Creating 2nd capturing group which has rest of the values here.

Upvotes: 2

The fourth bird
The fourth bird

Reputation: 163352

You can use

^.*/\K[^/\r\n]*/[^/\r\n]*$
  • .*/ Match the last occurrence of /
  • \K Forget what is matched until so far
  • [^/\r\n]*/[^/\r\n]* Backtrack to match one occurrence of / using a negated character class matching any char other than a forward slach or a newline
  • $ End of string

And replace with a semicolon and the full match using ;$0

Regex demo

enter image description here

Upvotes: 2

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626845

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.

Upvotes: 2

Related Questions