How to find / replace text between slashes in notepad ++?

How to find text between the second and fourth lines in the path, for example /folder/subfolder-1/subfolder-2/subfolder-3 ? I am trying to replace this with something like /folder/new-folder/subfolder-3 .

Most important to me is the ability to find the part after the nth slash.

I tried regex /((.*?)/){3} but it does not work.

+6
source share
6 answers

Using the meta symbol Reset Match \K you can do this in a simpler way.

Search:

 /.*?/\K(.*?/){2} 

Replaced by:

 new-folder/ 
+3
source

One way to do this is to use this line in the template to replace

 (/.+?)(/.+?){2}(/\S+) 

And use this in your template to replace it

 $1/new-folder$3 

From the line:

 /folder/subfolder-1/subfolder-2/subfolder-3 
  • (/.+?) will match /folder as $ 1

  • (/.+?){2} will match /subfolder-1/subfolder-2 as $ 2 (not used)

  • (/\S+) will match anything that is not space, in this case /subfolder-3 as $ 3

Leaving you in the room to insert a new folder between them.

+2
source

To find text between the second and fourth slashes, you can use the regular expression ^(/[^/]*/)([^/]*/[^/]*) , after which you can refer to the text between slashes with \2 when replacing text.

To save the text before the slash, you can enter something like \1myNewTextBetweenSlashes2and4 .

+1
source

How can I just point to slash?

  • Find what: (/[^/]+/)[^/]+/[^/]+
  • Replace with: $1new-folder
+1
source

{2} shows 2 levels after the first level is reflected new-folder

Search:

 (\/.*?\/)(.*?\/){2}(.*) 

Replace:

 $1new-folder/$3 

Demo: https://regex101.com/r/XIA3IN/3

0
source

In Notepad ++ Search by:

 (/[^/]+)(?:/[^/]+/[^/]+/)(.*) 

And replace as follows:

 \1\/new-folder/\2 

Make sure: .matches newline is not checked

0
source

Source: https://habr.com/ru/post/1014158/


All Articles