0

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?

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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"
    

2 Comments

it would suit me much better to be just a single regex find and replace, is there no way to combine our two since mine does it in one step but yours does it dynamically
Nope, sorry. Regex is powerful but it doesn't have that functionality - to do multiple different kinds of replacement in a single expression.
0

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

((.*)/)(.*)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.