We can do this in one replacement with some trickery. What we will do, we put several different cases in our template and make another replacement for each of them. The trick for this is that the replacement string should not contain alphabetic characters, but consists solely of "backlinks". In this case, those groups that did not participate in the match (because they were part of another case) will simply be recorded as an empty line and will not facilitate replacement. Let's start.
First, we want to delete everything to the last src/
(to simulate the behavior of your fragment) - use a fuzzy quantifier if you want to delete everything to the first src/
):
^.+/src/
We just want to give up on this, so there is no need to write anything or write anything.
Now we want to map subsequent folders to the last. We write the name of the folder, also match the final /
, but write the name of the folder and .
. But I did not say the literal text in the replacement string! In this way .
must come from capture. Here comes the assumption that your file always has the extension. We can get the period from the file name using lookahead. We will also use this look to make sure there is another folder ahead:
^.+/src/|\G([^/]+)/(?=[^/]+/.*([.]))
And we will replace this with $1$2
. Now, if the first alternative is caught, the groups $1
and $2
will be empty, and the leading bit will still be deleted. If the second choice catches, $1
will be the name of the folder, and $2
will be captured by the period. Sweet. \G
is an anchor that ensures that all matches are adjacent to each other.
Finally, we compare the last folder and everything that follows it, and write only the folder name:
^.+/src/|\G([^/]+)/(?=[^/]+/.*([.]))|\G([^/]+)/[^/]+$
And now we will replace this with $1$2$3
for the final decision. Demo
A conceptually similar option would be:
^.+/src/|\G([^/]+)/(?:(?=[^/]+/.*([.]))|[^/]+$)
replaced by $1$2
. I really only legalized the beginning of the second and third alternatives. Demo.
Finally, if Sublime uses Boost's extended format string syntax , you can actually conditionally get characters to replace (without spelling them magically) from the file extension):
^.+/src/|\G(/)?([^/]+)|\G/[^/]+$
Now we have the first alternative for everything up to src
(which should be deleted), the third alternative for the last slash and file name (which should be deleted), and the middle alternative for all the folders you want to keep. This time I put a slash instead of optionally at the beginning. With conditional replacement we can write .
there if and only if this slash was matched:
(?1.:)$2
Unfortunately, I cannot verify this right now, and I do not know an online tester that uses the Boost regex engine. But that should do the trick just fine.