如果目标存在,则正则表达式删除前导字符,否则返回整行

时间:2017-08-02 14:19:48

标签: regex notepad++

我正在使用Notepad ++并需要一个正则表达式来删除目标之前的所有字符(如果存在)。有些线路根本没有目标,在这种情况下我想保持线路不变。

In this example lets use a "<" character:

This line doesn't have one
This line does <keep>
This line also <and we'll keep all this
This line doesn't either
This line <has two< in it

Results:

This line doesn't have one
<keep>
<and we'll keep all this
This line doesn't either
<has two< in it

提前致谢

2 个答案:

答案 0 :(得分:0)

  • 控制 + ħ
  • 找到:^.*?(<.*)
  • 替换为:$1
  • 全部替换

<强>解释

^       : beginig of line
.*?     : 0 or more any character, not greedy
(<.*)   : group 1, '<' followed by 0 or more any character
$       : end of line
  • 请勿检查. matches newline

给定示例的结果:

This line doesn't have one
<keep>
<and we'll keep all this
This line doesn't either
<has two< in it

答案 1 :(得分:0)

您可以使用/^.*?(?=<)/m代替。

Demo