匹配除指定正则表达式之外的所有内容
我有一个巨大的文件,我想清除文件中的所有内容,除了与我的正则表达式匹配的内容。我知道我可以获得匹配项并提取它们,但我想保留我的文件并摆脱其他所有内容。
这是我的正则表达式:
"Id":\d+
我怎么说“匹配所有内容除了“Id”:\ d + ”。类似
!("Id":\d+) (pseudo regex)
的东西?
我想将它与正则表达式替换功能一起使用。用英语我想说:
获取所有非
"Id":\d+
的文本,并将其替换为空字符串。
I have a huge file, and I want to blow away everything in the file except for what matches my regex. I know I can get matches and just extract those, but I want to keep my file and get rid of everything else.
Here's my regex:
"Id":\d+
How do I say "Match everything except "Id":\d+
". Something along the lines of
!("Id":\d+) (pseudo regex)
?
I want to use it with a Regex Replace function. In english I want to say:
Get all text that isn't
"Id":\d+
and replace it with and empty string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
试试这个:
该模式将在同一行的多个 Id:Number 出现之间保留空格。如果每行只有一个
Id
,您可以从模式中删除\s?
。File.CreateText
将打开并覆盖现有文件。如果替换结果为空字符串,它将被跳过。否则结果将被写入文件。模式的第一部分匹配 Id:Number 次出现。它包括
.+
的替换,以匹配未出现 Id:Number 的行。替换使用$1
将匹配项替换为第一组的内容,即实际的Id
部分:(Id:\d+\s?)< /代码>。
Try this:
The pattern will preserve spaces between multiple Id:Number occurrences on the same line. If you only have one
Id
per line you can remove the\s?
from the pattern.File.CreateText
will open and overwrite your existing file. If a replacement results in an empty string it will be skipped over. Otherwise the result will be written to the file.The first part of the pattern matches Id:Number occurrences. It includes an alternation for
.+
to match lines where Id:Number does not appear. The replacement uses$1
to replace the match with the contents of the first group, which is the actualId
part:(Id:\d+\s?)
.嗯,在 perl-ish 正则表达式中,
\d
的反面是\D
。 .net 有类似的东西吗?well, the opposite of
\d
is\D
in perl-ish regexes. Does .net have something similar?抱歉,但我完全不明白你的问题是什么。将匹配项 grep 到新文件中不是很容易吗?
尤写道:
逻辑上的等价物是:
Sorry, but I totally don't get what your problem is. Shouldn't it be easy to grep the matches into a new file?
Yoo wrote:
A logical equivalent would be:
我以前没有使用过.net,但是在java中进行以下工作
会产生输出
虽然在真正意义上它不符合匹配除Id:\ d +之外的所有内容的标准,但它确实完成了工作
I haven't use .net before, but following works in java
produces output
Although in true sense it doesnt match the criteria of matching everything except Id:\d+, but it does the job