正则表达式删除某个字符之后的所有内容(注释)

发布于 12-12 20:03 字数 433 浏览 0 评论 0原文

我有一个正则表达式,用于删除特定字符(分号)之后的所有内容。

        var regex = new Regex(@";(.*)", RegexOptions.Singleline);
        tb.Text = regex.Replace(tb.Text, "");

它似乎工作正常,但有时它会删除文本框的整个文本。例如,所有这些代码都被删除:

;fgkdfgdfgd
;dfgdfkghdfgdf
;sdgfsdfsdfsdf
;dfgdfgdfg

#dont remove this          ;fgdfgdfg

“#dont remove this”应该保持不变,因为它不在分号之后,但事实并非如此?我的正则表达式有问题吗?

这个想法是删除或修剪文件中的所有注释。

I have a regex which I am using to remove everything after a specific character, semi-colon.

        var regex = new Regex(@";(.*)", RegexOptions.Singleline);
        tb.Text = regex.Replace(tb.Text, "");

It seems to work fine, but at times it removes the entire text of the text box. For example all of this code is removed:

;fgkdfgdfgd
;dfgdfkghdfgdf
;sdgfsdfsdfsdf
;dfgdfgdfg

#dont remove this          ;fgdfgdfg

the "#dont remove this" should stay intact because it isn't after the semi-colon, but it doesn't? Is something wrong with my regex?

The idea is to remove or trim all comments from a file.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

长亭外,古道边2024-12-19 20:03:27

尝试(评论后更新):

tb.Lines = (
    from l in tb.Lines 
    let x = l.IndexOf (';') 
    select (x >= 0 ? l.SubString (0, x) : l)
).ToArray();

这也应该比正则表达式运行得更快......

try (UPDATE after comment):

tb.Lines = (
    from l in tb.Lines 
    let x = l.IndexOf (';') 
    select (x >= 0 ? l.SubString (0, x) : l)
).ToArray();

This should run faster than the Regex too...

追我者格杀勿论2024-12-19 20:03:27

这是因为您使用的是 RegexOptions.Singleline,因此 . 匹配新行。

It's because you are using RegexOptions.Singleline and therefore the . is matching new lines.

萌面超妹2024-12-19 20:03:27

RegexOptions.Singleline 并不像您所期望的那样将匹配限制为单行。事实上,其目的恰恰相反。它允许 . 元字符匹配换行符,从而更容易查找跨越多行的匹配项。只要放下它,你就会没事的。

RegexOptions.Singleline doesn't limit the match to a single line as you might expect. In fact, its purpose is just the opposite. It allows the . metacharacter to match newlines, making it easier to find matches that span across multiple lines. Just drop that and you should be fine.

黑色毁心梦2024-12-19 20:03:27

问题很简单 - 您误解了 RegexOptions.SingleLine

SingleLine 告诉模式 . 可以匹配换行符。 在此处了解有关 RegexOptions 的更多信息< /a>.

您当前的结果是单个匹配(从第一个 ; 到整个字符串的末尾)。

您只需删除 RegexOptions.SingleLine 即可,您的模式会将每个注释匹配到行尾。

The issue is very simple - you misunderstood RegexOptions.SingleLine.

SingleLine tells the pattern that . can match line breaks. Read more about RegexOptions here.

Your current result is a single match (from the first ; to the end of the entire string).

You should just remove the RegexOptions.SingleLine and your pattern will match each comment to the end of the line.

药祭#氼2024-12-19 20:03:27

您可以使用以下命令轻松完成此操作:

tb.Text = tb.Text.Substring(0, tb.Text.IndexOf(';'));

这应该比使用正则表达式运行得更快...

如果您的文本框是多行,您可以使用:

s = TextBox1.Text;
string ret = "";
s.Split('\n').ToList().ForEach(p=>ret += p.Substring(0, p.IndexOf(';')) + "\n");
TextBox1.Text = ret;

You could do it easily using this:

tb.Text = tb.Text.Substring(0, tb.Text.IndexOf(';'));

This should run faster than using Regex...

If your textbox is multiline you could use:

s = TextBox1.Text;
string ret = "";
s.Split('\n').ToList().ForEach(p=>ret += p.Substring(0, p.IndexOf(';')) + "\n");
TextBox1.Text = ret;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文