简体   繁体   English

使用RegEx类从电子邮件主题中删除“ RE:”(类似)

[英]Removing “RE:” (and alikes) from email subject using RegEx class

I need to remove the leading RE: from a subject and it's definitely not my day today, because I don't get the match and obviously am missing something simple. 我需要从主题中删除领先的RE:绝对不是我今天的事情,因为我没有找到比赛,很明显缺少简单的东西。 My predecessor made it abundantly clear that the Substring approach works but I just can't stand bad technique. 我的前任非常清楚地表明Substring方法是可行的,但我不能忍受糟糕的技术。 :) :)

//emailSubject = emailSubject.Substring(4);
emailSubject = Regex.Replace(emailSubject, @"^.{0, 3}:\s", "");

The way I see it, we should find the beginning of the string and get between zero and three characters followed by a colon and a space. 从我的角度来看,我们应该找到字符串的开头,并获得介于0和3个字符之间的空格,然后是冒号和空格。 Then, this junk should be removed. 然后,该垃圾将被清除。

The computer disagrees, though, and I'm out of ideas. 但是,计算机不同意,我没有主意。 What simple thing do I miss here?! 我在这里想念什么简单的事情?

删除逗号后的空格:

@"^.{0,3}:\s"

You can remove all sort of stuffs such as re and fwd with the following regex: 您可以使用以下正则表达式删除所有内容,例如refwd

^(?>(?:re|fwd) *: *)*

The regex above is to be used with RegexOptions.IgnoreCase . 上面的正则表达式将与RegexOptions.IgnoreCase一起使用。

It can even take care of multiple reply and forward, such as: 它甚至可以处理多个答复和转发,例如:

re: re : RE: fwd  : re: fwd:  About meeting tomorrow

If you are concerned about multiple FWD chains (and alikes) and multiple client languages: 如果您担心多个FWD链(类似)和多种客户端语言:

    public static string ClearSubject(string originalSubject)
    {
        Regex regex = new Regex(@"^([\[\(] *)?(RE?S?|FYI|RIF|I|FS|VB|RV|ENC|ODP|PD|YNT|ILT|SV|VS|VL|AW|WG|ΑΠ|ΣΧΕΤ|ΠΡΘ|תגובה|הועבר|主题|转发|FWD?) *([-:;)\]][ :;\])-]*|$)|\]+ *$", RegexOptions.IgnoreCase);
        originalSubject = regex.Replace(originalSubject, string.Empty);

        if (regex.IsMatch(originalSubject))
        {
            return ClearSubject(originalSubject);
        }

        return originalSubject;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM