Question

Okay, I am working on a profile which contains many IDs for objects in general.

All the initial code had to be edited so more functions could be added, and it's about 1200 ID's I would have to manually edit one by one. So I thought maybe Notepad++ could use RegEx functions to do the whole editing for me.

The problem however, is that I need to retrieve the ID from line 2 which is whitin quotes and then put this ID in line 1 which has to be between parentheses. This step will be repeated multiple times so instead of being line 2 and line 1, the next set will be line 4 and 3 and so on.

Example:

Line 1: < InbagCount(11111) />
Line 2: < ItemID="12345" />
Line 3: < InbagCount(11111) />
Line 4: < ItemID="543" />

Now, I need to replace all the 11111 with the next line's ItemID.

this is the code I've been using which matches the text inside ItemID=""

(?<=ItemID=")(?:\\.|[^"\\])*(?=")

It works to find them but when I click the Replace button this is what happens:

InbagCount(?<=ItemID="?:\.|[^"\]*?=")

Maybe Notepad++ is not the tool for this job.

Does anyone have an idea of what I can use?

Thanks in advance, I would be very appreciated if you can help me :)

Was it helpful?

Solution

I can't test with Notepad++ because I don't Windows around, but if I understand your request correctly, it's a simple regex. The key is matching across line boundaries. You match two lines at once, use a capture group for the text in question and the reconstruct the two lines.

So, for example, you can match with:

.*?ItemID="(\d+)" />

and replace with:

< InbagCount($1) />\n< ItemID="$1" />\n

as shown in this website. You may be able to actually convert your code using that website.

OTHER TIPS

you can use notepad++ macro instead of regex. place your crusor on your second line, press home then click menu macro > start recording, then do the following steps :

  1. ctrl+right_arrow(until your number, three time maybe)
  2. ctrl+shift+right_arrow
  3. ctrl+c(copy)
  4. up_arrow
  5. home
  6. ctrl+right_arrow(until your number, three time maybe)
  7. ctrl+shift+right_arrow
  8. ctrl+v(paste)

then stop recording, and pres run macro multiple times > until the end of file > run

This will work in Notepad++ on Windows as you so desired

Assuming this is the text:

< InbagCount(11111) />
< ItemID="12345" />
< InbagCount(11111) />
< ItemID="54321" />

Ctrl+H:

In "Find what :" \(([0-9]*).*\r\n.*"([0-9]*)"

In "Replace with :" \(\2\) />\n< ItemID="\2"

The newline issue is explained here: Notepad++ newline in regex

There are actually two regex captures, \1 for the 11111 tag, and \2 for the ItemID number. The one for the 11111 tag wasn't necessary.

This is a bit more involved, but it presumes some strictness (that the capture expression works on line 2), that

  • A newline separates the ItemID and the InbagCount line.
  • The replacement is contained within parentheses on the previous line
  • ItemID is surrounded by double quotes
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top