Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Notepad++ regex How to Replace third occurrence of slash & bypass the first and seconds?

I have strings as below.
ABC//DEG//IJK//LMN//OPQ//rstuvwxyz
BCA//EGD//JKI//MNL//PQO//stuvwxyzr
ACB//DGE//IJK//LNM//OQP//rstuvwxyz
ABC//DEG//IJK//LMN//OPQ//rstuvwxyz
CAB//GDE//KIJ//NLM//QOP//rstuvwxyz
BAC//EDG//JIK//MLN//POQ//rstuvwxyz

I want it to be like this,

ABC//DEG//IJK\\LMN//OPQ//rstuvwxyz
BCA//EGD//JKI\\MNL//PQO//stuvwxyzr
ACB//DGE//IJK\\LNM//OQP//rstuvwxyz
ABC//DEG//IJK\\LMN//OPQ//rstuvwxyz
CAB//GDE//KIJ\\NLM//QOP//rstuvwxyz
BAC//EDG//JIK\\MLN//POQ//rstuvwxyz

I have tried
Find what ^.+?\K//
Replace with: \\\\
But this will only change the first occurrence of slash,,,
And I have tried this,
Find what: ^.+\K//
Replace with: \\\\
And this will replace the final slash at the end of every lines ..
I tried also {3} curly brackets with numbers, but no benifits.... Thanks in advance for your helps....

like image 450
Haji Rahmatullah Avatar asked Nov 30 '25 20:11

Haji Rahmatullah


2 Answers

You can use

^(?:.*?\K//){3}

Replace with \\\\. See the regex demo online.

Details:

  • ^ - start of a line
  • (?:.*?\K//){3} - three occurrences of any zero or more chars other than line break chars, as few as possible, then \K (match reset operator) discards all the text matched so far and then // substring is only kept in the match value.

See the demo and settings screenshot:

enter image description here

like image 108
Wiktor Stribiżew Avatar answered Dec 03 '25 12:12

Wiktor Stribiżew


If you have only uppercase chars A-Z as in the example data, you can use a character class [A-Z]+

^(?:[A-Z]+//){2}[A-Z]+\K//
  • ^ Start of string
  • (?:[A-Z]+//){2} Repeat 2 times matching uppercase chars A-Z and //
  • [A-Z]+\K// Match 1+ uppercase chars A-Z, clear the match buffer and match //

Regex demo

In the replacement use \\\\

like image 44
The fourth bird Avatar answered Dec 03 '25 13:12

The fourth bird



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!