Aequitas
Aequitas

Reputation: 2265

Dynamic Regex find and replace pattern

My aim is to turn something like this:

src/com/company/folder/anotherfolder/manyfolders/filename.java

into this:

a few words com.company.folder.anotherfolder.manyfolders filename "description"

I've got it mostly to work with the following find and replace:

Find: (.*?)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)/(.*?)(\..*)

Replace: a few words $2\.$3\.$4\.$5\.$6\.$7\.$8\040$9\040"description"

Which works okay, however I may have different number of folders, so it won't work in that case.

How can I make it so that it works for any number of folders?

Upvotes: 0

Views: 434

Answers (3)

Jerry
Jerry

Reputation: 71598

You are carrying out two distinct processes here:

  1. Replacing all / by .
  2. Adding text before and after the replacement result and some other minor changes.

This is not possible with a single regex and if you still want to do it with the find/replace function of notepad++, you will need to do two replaces. I suggest the below replaces:

  1. Replace / by . (no need for regex here, but if you have / you don't want to replace, then it becomes more complex and you might to use a regex like (?:(?!^)\G|)\K/(?=[^.\r\n]*\.) for the find and . for the replace).

  2. Regex replace of

    [^.\r\n]+\.((?:[^.\r\n]+\.)+[^.\r\n]+)\.([^.\r\n]+)\.[^.\r\n]+
    

    And replace with

    a few words $1 $2 "description"
    

I am using this regex with the assumptions that folder and filenames can contain spaces and occupy a full line.

Upvotes: 1

nneonneo
nneonneo

Reputation: 179707

Consider using a few regexes in a row:

  1. Convert slashes

    /
    

    =>

    .
    
  2. Convert the rest

    ([a-zA-Z0-9\.]+)\.(\w+)\.java
    

    =>

    a few words $1 $2 "description"
    

Upvotes: 1

Evan Knowles
Evan Knowles

Reputation: 7511

You'd need to repeat on the / character - have you tried something like:

((.*)/)(.*)

Upvotes: 0

Related Questions