如何删除除“01 - 10”模式以外的所有内容,反之亦然?

How to remove everything except "01 - 10" pattern and vice-versa?

我想从文件中删除除 01 - 10 模式之外的所有内容,包括数字、字符、特殊字符。例如:

01 - 10
This is the first one :1
02 - 20
This is the second one -2
03 - 30
This is the third one "3
04 - 40
This is the forth one ;4
05 - 50
This is the fifth one .5

我使用的正则表达式是 [^\d\d\s-\s\d\d],请记住通过 selecting \d\d\s-\d\d 和使用字符集 charet(^ ). 但是使用这个我得到:

01 - 10
     1
02 - 20
     -2
03 - 30
     3
04 - 40
     4
05 - 50
     5

但我希望结果是:

01 - 10
02 - 20
03 - 30
04 - 40
05 - 50
     

I.E.,我想要
01 - 10 模式,并且
不包括问题中提到的任何个人 :1 和 -2 以及 "3 和 ;4 和 .5在顶部。

反之亦然,即 select 除“01 - 10”模式外的每一行
例如:

This is the first one :1
This is the second one -2
This is the third one "3
This is the forth one ;4
This is the fifth one .5

我想知道 01 - 10 大小写的正则表达式模式,反之亦然,这样我就可以将分别生成的结果保存在单独的文件中。

使用这个:

^(?!\d\d\s-\s\d\d).*

演示:https://regex101.com/r/t4qrh0/1

您可以使用这个正则表达式:

^(?!\d{2}\h*[:-]\h*\d{2}\h*$).*[\r\n]

RegEx Demo

正则表达式详细信息:

  • ^: 开始
  • (?!:开始否定前瞻
    • \d{2}: 匹配2个数字
    • \h*[:-]\h*:匹配0个或多个水平空格后跟:-后跟0个或多个水平空格
    • \d{2}: 匹配2个数字
    • \h*:匹配0个或多个空格
    • $: 行尾
  • ):结束否定前瞻
  • .*: 匹配任何东西
  • [\r\n]: 匹配 1+ 个换行符
  • 替换为空字符串,删除所有匹配行

反向删除

要删除数字对行,您可以使用:

^(?=\d{2}\h*[:-]\h*\d{2}\h*$).*[\r\n]+

RegEx Demo 2