Search code examples
regexnotepad++

Find nth occurrence of a symbol and replace using regular expression


I have a file with more than 20K lines and in each line we have exactly 151 '@' symbols as a delimiter. I need to replace value between 104th, 105th and 105th and 106th occurrence of '@' symbol. I tried with Excel but having issues with date fields and felt replacing in Notepad++ is easy. Could anyone provide inputs on the regex? I tried with replacing with nth occurrence of the symbol but it didn't work.

Sample Line

9007;624;@0@0@1@Company & Co. "XYZ" ;Comp & Co. HG;@@@Ger;Ger;@@@@0@0@@@@@@@@@uid=XYZ@@@@@Gery;@0@@@@@0@@@@@@@T@@@@@0@0@0@0@0@0@0@0@0@0@0@@@@@@@@@@@@@@@@1@1@1@1@1@@@@@@@@@@@@@@@@@@@@@@@@@@08.007.001.001 Bs;08.007.001.004 Zahlungen;üüüüüüü;äääää;@Sct;2006;Mar;da das Haus;@@@$S0et7xV0@568;536871132;36108;536871497;536871712;536871449;536907021;148000;@THA@@@uid=Compa@03/03/2016;12:41:00:00@doc@096@@0@@1@@@OHG | 0624 | Ger | 9007 | Ger;@@2005;1953;@B48D3B01257184408AF2F2D69BAC1DEF000000000000@268471076@@@@@@@0@@@@@@07/24/2012;07:39:00:00@@@@@@@@@@@@$S0et7xV0@0@[email protected]

Highlighted in bold needs to be replaced. Appreciate anyone's insights on this.

Thanks!


Solution

  • You could use the following replacement instruction (press Ctrl+H to open Find/Replace dialog):

    Find what: ^((?:[^@]*@){104})[^@]*@[^@]*
    Replace with: \1Hello@World
    Search mode:
    ⦿ Regular expression

    Replace All

    Here the two texts are replaced by "Hello" and "World". Adapt as needed.

    Break down of the regex:

    • ^ matches the start of a line
    • [^@]*@ matches a sequence of non-@ characters, followed by one @.
    • (?: ){104}: repeats 104 times the pattern enclosed within (?: )
    • ( ): captures the matched text in a capture group that can be referenced with \1.