Tom
Tom

Reputation: 2472

Regex remove everything except what I search for

Using Notepad++, I have a file that contains hundreds of lines like:

Updates\Microsoft .NET Framework 2.0 Service Pack 2\KB946503]

Updates\Microsoft .NET Framework 2.0 Service Pack 2\KB946504]

I want to find the KB numbers and replace the entire document with the KB number, so it looks like this:

KB946503

KB946504

So far I can find the KB numbers using (?=\b\w+\b)\w*kb\w* but I do not know what to put in the replace field, I've tried \1 and $1 but that doesn't work! :)

Upvotes: 1

Views: 1445

Answers (1)

Wiktor Stribiżew
Wiktor Stribiżew

Reputation: 626699

Use

^.*\b(KB\d+)\]$

And replace with \1.

The regex will find all lines that end with KB+digits+], and the KBxxxxxx will be restored with the \1 backreference to the captured substring.

enter image description here

If you just want to find the last KBxxxx on a line, use

^.*\b(KB\d+).*$

To remove all texts before a KB, use

^[\s\S]*?\b(KB\d+).*$

However, the "tail" should be remove manually.

Upvotes: 1

Related Questions