Notepad++ and xml - replacing / in closing element tag

I have an XML file (1000s of records, simplified here), structure (e.g. say):

<LIST> <ITEM_0> <NAME>Item Name</NAME> </ITEM_0> ... <ITEM_9999> <NAME>Item Name</NAME> </ITEM_9999> </LIST> 

I need result:

<LIST> <ITEM> <ID>0</ID> <NAME>Item Name</NAME> </ITEM> ... <ITEM> <ID>9999</ID> <NAME>Item Name</NAME> </ITEM> </LIST> 

Using Regex:

Find: \<ITEM_(.*)(>) Replace: ITEM>\n<ID>\1\</ID> 

I get:

<LIST> <ITEM> <ID>0</ID> <NAME>Item Name</NAME> </ITEM> <ID>0</ID> <-- This line not wanted ... <ITEM> <ID>9999</ID> <NAME>Item Name</NAME> </ITEM> <ID>9999</ID> <-- This line not wanted </LIST> 

It's replacing </ITEM> AS WELL even though (I think) I'm asking it to only replace <ITEM>- what am I doing wrong/how to fix? I may be missing something regarding grouping (or 'greedy'?) but not sure what and have looked all over for similar. There's a million ways to cut and dice it with something else, but it just bugs me getting so close but not there with NPP.

Help appreciated- thanks.


Yes, it's likely that the .* is too "greedy" and captures as many characters as it can; you need the opposite – the shortest possible match instead.

One method would be to use [^>]* instead – this would still match as many as possible, but only until the first >, so <ITEM_([^>]*)> would only match the opening tag and nothing more.

Depending on regex syntax, .*? might also work – this explicitly switches the * to "non-greedy".

Category: notepad++ Time: 2016-07-31 Views: 3

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development


Front-end development


development tools

Open Platform

Javascript development

.NET development

cloud computing


Copyright (C), All Rights Reserved.

processed in 0.653 (s). 12 q(s)