匹配除指定正则表达式之外的所有内容

发布于 2024-09-26 04:28:31 字数 384 浏览 0 评论 0原文

我有一个巨大的文件,我想清除文件中的所有内容,除了与我的正则表达式匹配的内容。我知道我可以获得匹配项并提取它们,但我想保留我的文件并摆脱其他所有内容。

这是我的正则表达式:

"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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很快妥协 2024-10-03 04:28:31

试试这个:

string path = @"c:\temp.txt"; // your file here
string pattern = @".*?(Id:\d+\s?).*?|.+";
Regex rx = new Regex(pattern);

var lines = File.ReadAllLines(path);
using (var writer = File.CreateText(path))
{
    foreach (string line in lines)
    {
        string result = rx.Replace(line, "$1");
        if (result == "")
            continue;

        writer.WriteLine(result);
    }
}

该模式将在同一行的多个 Id:Number 出现之间保留空格。如果每行只有一个 Id,您可以从模式中删除 \s?File.CreateText 将打开并覆盖现有文件。如果替换结果为空字符串,它将被跳过。否则结果将被写入文件。

模式的第一部分匹配 Id:Number 次出现。它包括 .+ 的替换,以匹配未出现 Id:Number 的行。替换使用 $1 将匹配项替换为第一组的内容,即实际的 Id 部分:(Id:\d+\s?)< /代码>。

Try this:

string path = @"c:\temp.txt"; // your file here
string pattern = @".*?(Id:\d+\s?).*?|.+";
Regex rx = new Regex(pattern);

var lines = File.ReadAllLines(path);
using (var writer = File.CreateText(path))
{
    foreach (string line in lines)
    {
        string result = rx.Replace(line, "$1");
        if (result == "")
            continue;

        writer.WriteLine(result);
    }
}

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 actual Id part: (Id:\d+\s?).

鯉魚旗 2024-10-03 04:28:31

嗯,在 perl-ish 正则表达式中, \d 的反面是 \D 。 .net 有类似的东西吗?

well, the opposite of \d is \D in perl-ish regexes. Does .net have something similar?

强者自强 2024-10-03 04:28:31

抱歉,但我完全不明白你的问题是什么。将匹配项 grep 到新文件中不是很容易吗?

尤写道:

获取所有非“Id”的文本:\d+ 并将其替换为空字符串。

逻辑上的等价物是:

获取与 "Id":\d+ 匹配的所有文本并将其放入新文件中。用新文件替换旧文件。

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:

Get all text that isn't "Id":\d+ and replace it with and empty string.

A logical equivalent would be:

Get all text that matches "Id":\d+ and place it in a new file. Replace the old file with the new one.

时光清浅 2024-10-03 04:28:31

我以前没有使用过.net,但是在java中进行以下工作

System.out.println("abcd Id:12351abcdf".replaceAll(".*(Id:\\d+).*","$1"));

会产生输出

Id:12351

虽然在真正意义上它不符合匹配除Id:\ d +之外的所有内容的标准,但它确实完成了工作

I haven't use .net before, but following works in java

System.out.println("abcd Id:12351abcdf".replaceAll(".*(Id:\\d+).*","$1"));

produces output

Id:12351

Although in true sense it doesnt match the criteria of matching everything except Id:\d+, but it does the job

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文