1

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 :)

3 Answers3

0

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

Angga
  • 2,305
  • 1
  • 17
  • 21
  • Haha, although that's a clever suggestion, it will not work. The reason is the ItemID will sometimes be 3 characters long and sometimes 4 or 5 characters long. Thanks for the try though! – user2649788 Aug 04 '13 at 04:42
  • thats why i use ctrl, this ignore the length – Angga Aug 04 '13 at 04:49
  • Lol, that's amazing, I did not know the Ctrl trick. I guess it could also be done this way but another gentleman also posted a solution with RegEx which resulted in "less" work. Thank you too though, this should work just fine! – user2649788 Aug 04 '13 at 04:57
0

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.

Peter Alfvin
  • 28,599
  • 8
  • 68
  • 106
  • You sir are my hero, kinda. That website you linked allowed me to do what I couldn't do in Notepad++. For some reason, when I entered exactly the same search it would not accept the \n even though I had regular expression ticked. Anyway, thank you a ton. – user2649788 Aug 04 '13 at 04:54
0

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
Community
  • 1
  • 1
Kevin Lee
  • 718
  • 6
  • 19