Ronan Paixão
Ronan Paixão

Reputation: 8675

How to replace a group-of-groups RegExp in Notepad++

I have some text like this:

1|Some things|US|192||||||1||
2|Some other things|US|128|3|||5|1|1|3|
3|Whatever else|US|128||15||||||

And I'd like to replace it in Notepad++ with something like:

1|Some things|US|192|+++++1++
2|Some other things|US|128|3+++5+1+1+3+
3|Whatever else|US|128|+15++++++

But I couldn't find some proper way to do it. I managed to build a RegExp that matches the string:US\|(\d+)\|((\d*)\|)+ but I couldn't find how to build the replacement string to match this group-of-groups approach.

The hard part for me was excluding the first number after the "US". The only way out I could see was replacing (?<![a-zA-Z])\|(?![a-zA-Z]) for + and then replacing US\|(\d+)\+ back to US|\1|.

Is it possible to do such a replacement with Notepad++'s builtin or TextFX's replacement tools?

Upvotes: 2

Views: 475

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89557

You can use this pattern:

(?:\G(?!^)|\|US\|\d+\|)\d*\K\|

With + as replacement string.

For each line the scenario is the same:

  • the first match uses the second branch of the alternation: \|US\|\d+\| and match the first pipe
  • other matches use the first branch with the \G anchor that ensures the contiguity.
  • when the last pipe is reached, the contiguity is broken and the first branch can't no more succeed.

The \K feature removes all previous matched characters from the match result and only the pipe is replaced.

Upvotes: 4

Related Questions