Notepad++正则表达式 – 查找或删除特定数字

huangapple go评论56阅读模式
英文:

Notepad++ regex - find or remove specific numbers

问题

I would like to remove specific numbers from a text file, the list of the numbers are in order from 1 to 192061.
我想从文本文件中删除特定的数字,这些数字按顺序从1到192061排列。

I need to remove a group of numbers, some examples: "61186","95492","158740","151276","92709"
我需要删除一组数字,一些示例:"61186","95492","158740","151276","92709"

And my list in total is around of 13k.
我的列表总共大约有13,000个。

I have tried this: (61186)|(95492)|(158740)|(151276)|(92709).
我尝试过这个:(61186)|(95492)|(158740)|(151276)|(92709)。

But in the list I have: 61186 and 161186 and it will not match.
但在列表中,我有61186和161186,它们不匹配。

英文:

I would like to remove specific numbers from a text file, the list of the numbers are in order from 1 to 192061.
I need to remove a group of numbers, some exemples : "61186","95492","158740","151276","92709"
And my list in total is around of 13k.

I have tried this : (61186)|(95492)|(158740)|(151276)|(92709).
But in the list I have : 61186 and 161186 and it will not match.

答案1

得分: 1

A simple number range generated regex for 1 - 192061 would be:

(?<!\d)(?:[1-9]\d{0,4}|1[0-8]\d{4}|19[01]\d{3}|1920[0-5]\d|19206[01])(?!\d)

It's doubtful you should do it without the look ahead/behind assertions,
but the regex could be rearranged like this:

1[0-8]\d{4}|19[01]\d{3}|1920[0-5]\d|19206[01]|[1-9]\d{0,4}

英文:

A simple number range generated regex for 1 - 192061 would be:

(?&lt;!\d)(?:[1-9]\d{0,4}|1[0-8]\d{4}|19[01]\d{3}|1920[0-5]\d|19206[01])(?!\d)

https://regex101.com/r/YL4GS3/1

It's doubt full you should do it without the look ahead/behind assertions,
but the regex could be re-arrange like this :

1[0-8]\d{4}|19[01]\d{3}|1920[0-5]\d|19206[01]|[1-9]\d{0,4}

https://regex101.com/r/7qe299/1

答案2

得分: 1

只需使用 ^$ 锚点来匹配行的开头和结尾,并在所有替代项周围使用捕获组,而不是每个替代项都使用一个,例如:

^(61186|95492|158740|151276|92709|438)$

在评论中使用您的数据的演示

英文:

Simply use ^ and $ anchors to match start/end of the line and use capture group around all the alternatives rather than for each one, e.g.:

^(61186|95492|158740|151276|92709|438)$

Demo with your data from a comment

huangapple
  • 本文由 发表于 2023年5月7日 02:40:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76190540.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定