Notepad++ regex add lines, copy string

notepadregex

I want to search replace & strings in thousands of wavefront *.mtl files to add many new Texture links. Is this even possible with Notepad++ regex? I´ve already altered these files very often with regex, but this task is way over my head!

At the current state I got many Materialparts in every file looking like this:

newmtl Material__28
    Ns 0.000000
         ︙
    Kd 1.000000 1.000000 1.000000
    Ks 0.000000 0.000000 0.000000
    Ke 0.000000 0.000000 0.000000

    map_Kd textures\wall_exterior_wood_02.png

where the dots represent text that I don't want to change
(and that doesn't contain the string map_Kd).

And afterwards it should contain the NRM & Spec PNGs named exactly like the first map_Kd.

Example:

newmtl Material__28
    Ns 0.000000
         ︙
    Kd 1.000000 1.000000 1.000000
    Ks 0.000000 0.000000 0.000000
    Ke 0.000000 0.000000 0.000000

    map_Kd   textures\wall_exterior_wood_02.png
    map_bump textures\wall_exterior_wood_02_nrm.png
    map_bump textures\wall_exterior_wood_02_nrm alternative.png
    map_Ks   textures\wall_exterior_wood_02_spec.png

I've added spaces to the map lines for clarity.
Important is that the naming of the PNGs should be the same except for the prefixes "map_bump" and "map_ks" and the postfixes "_nrm", "_nrm alternative", and "_spec".

Best Answer

In Notepad++.

Use ctrl+h (replace)

Select "Regular Expression" radio button in the Search Mode box in the bottom left (making sure the matches newline tickbox is unticked)

Find:

map_Kd\s+(\S+)\.png

Replace:

map_Kd \1.png\n map_bump \1_nrm.png\n map_bump \1_nrm_alternative.png\n mapKs \1_spec.png

The find expression looks for "map_Kd" followed by one or more spaces ("\s+") then keeps one or more non-space characters ("(\S+)") until it hits ".png"

The replace puts the string you kept in wherever "\1" appears. I've also included "\n" newlines and spacing to align things as in your example.

Related Question