How to replace all string between second and third occurance of pipe character using regex?

杀马特。学长 韩版系。学妹 提交于 2021-02-20 05:28:05

问题


I would like to replace all the characters occurring between second and third pipe character. Example:

|hello|welcome,to|

In this I want replace welcome,to with a blank value, with a huge file to be replaced. I need a regex pattern to be used in notepad++.


回答1:


Assuming you're parsing each line, i.e. a token cannot include a newline:

Find what:

^((?:[^|\n]*\|){2})[^|\n]*+\|

Replace with:

$1|

Description

  • ^- matches the start of line
  • ((?:[^|\n]*\|){2}) - matches and captures in group 1:
    • (?:[^|\n]*\|){2} the following expression repeated twice
    • [^|\n]* - any character except a pipe or a newline
    • \| - followed by a pipe
  • [^|\n]*+ - the token you want to remove (any char except pipe or newline)
  • \| - followed by a pipe



回答2:


In notepad++ use this regex for search:

^([^|]*\|[^|]*\|)[^|]*

And replace by:

$1

([^|]*\|[^|]*\|) matches and captures text before 2nd | into a capturing group #1.

RegEx Demo



来源:https://stackoverflow.com/questions/40067431/how-to-replace-all-string-between-second-and-third-occurance-of-pipe-character-u

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!