【发布时间】:2021-07-08 14:14:37
【问题描述】:
我想从文件中删除所有内容,包括数字、字符、特殊字符,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],请记住通过选择\d\d\s-\d\d 并使用字符集charet(^) 来匹配除“01 - 10”模式之外的所有内容。
但使用这个我得到:
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
IE,我希望,
01 - 10 模式,并且,
不包括任何个人:1 和 -2 以及“3 和 ;4 和 .5,如顶部问题中所述。
反之亦然,即选择除“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 情况下的正则表达式模式,反之亦然,这样我就可以将两个单独生成的结果保存在单独的文件中。
【问题讨论】:
-
使用
^(?!0(\d) - \g{1}0$).*进行匹配并替换为空字符串。 -
感谢您的快速回复,但请您详细说明一下,以便我了解此正则表达式的工作原理
-
我正在使用 ATOM 文本编辑器
-
我对此还有一个疑问 - 它是否也适用于 01:10 之类的模式,并稍作修改,例如 ^(?!0(\d):\g{1}0$).* ?
标签: regex atom-editor