如何从字符串内容中删除一些特殊的单词?

How remove some special words from a string content?

我有一些包含表情符号图标代码的字符串,例如 :grinning::kissing_heart::bouquet:。我想处理它们以删除表情符号代码。

例如,给定:

Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:

我想要这个:

Hello , how are you? Are you fine?

我知道我可以使用这个代码:

richTextBox2.Text = richTextBox1.Text.Replace(":kissing_heart:", "").Replace(":bouquet:", "").Replace(":grinning:", "").ToString();

但是,我必须删除 856 个不同的表情符号图标(使用此方法,需要对 Replace() 进行 856 次调用)。有没有其他方法可以做到这一点?

string Text = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:";

我会这样解决的

List<string> Emoj = new List<string>() { ":kissing_heart:", ":bouquet:", ":grinning:" };
Emoj.ForEach(x => Text = Text.Replace(x, string.Empty));

更新 - 参考细节评论

另一种方法:仅替换现有的 Emojs

List<string> Emoj = new List<string>() { ":kissing_heart:", ":bouquet:", ":grinning:" };
var Matches = Regex.Matches(Text, @":(\w*):").Cast<Match>().Select(x => x.Value);
Emoj.Intersect(Matches).ToList().ForEach(x => Text = Text.Replace(x, string.Empty));

但我不确定对于如此短的聊天字符串来说是否有那么大的区别,而且拥有易于 read/maintain 的代码更为重要。 OP 的问题是关于减少冗余 Text.Replace().Text.Replace() 而不是最有效的解决方案。

您可以使用 Regex 来匹配 :anything: 之间的单词。将 Replace 与函数一起使用,您可以进行其他验证。

string pattern = @":(.*?):";
string input = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet: Are you super fan, for example. :words not to replace:";
string output = Regex.Replace(input, pattern, (m) =>
{
    if (m.ToString().Split(' ').Count() > 1) // more than 1 word and other validations that will help preventing parsing the user text
    {
        return m.ToString();
    }
    return String.Empty;
}); // "Hello , how are you? Are you fine? Are you super fan, for example. :words not to replace:"

如果您不想使用使用 lambda 表达式的 Replace,您可以使用 \w,正如@yorye-nathan 提到的那样,只匹配单词。

string pattern = @":(\w*):";
string input = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet: Are you super fan, for example. :words not to replace:";
string output = Regex.Replace(input, pattern, String.Empty); // "Hello , how are you? Are you fine? Are you super fan, for example. :words not to replace:"

您不必替换所有 856 个表情符号。您只需替换出现在字符串中的那些。所以看看:

基本上你提取所有标记,即 : 和 : 之间的字符串,然后用 string.Empty()

替换它们

如果您担心搜索将 return 不是表情符号的字符串,例如 :some other text: 那么您可以进行散列 table 查找以确保替换上述找到的标记做的合适

我会结合使用一些已经建议的技术。首先,我将 800 多个表情符号字符串存储在数据库中,然后在运行时加载它们。使用 HashSet 将这些存储在内存中,这样我们就有了 O(1) 的查找时间(非常快)。使用正则表达式从输入中提取所有可能的模式匹配项,然后将每个匹配项与我们的哈希表情符号进行比较,删除有效的并留下用户自己输入的任何非表情符号模式...

public class Program
{
    //hashset for in memory representation of emoji,
    //lookups are O(1), so very fast
    private HashSet<string> _emoji = null;

    public Program(IEnumerable<string> emojiFromDb)
    {
        //load emoji from datastore (db/file,etc)
        //into memory at startup
        _emoji = new HashSet<string>(emojiFromDb);
    }

    public string RemoveEmoji(string input)
    {
        //pattern to search for
        string pattern = @":(\w*):";
        string output = input;

        //use regex to find all potential patterns in the input
        MatchCollection matches = Regex.Matches(input, pattern);

        //only do this if we actually find the 
        //pattern in the input string...
        if (matches.Count > 0)
        {
            //refine this to a distinct list of unique patterns 
            IEnumerable<string> distinct = 
                matches.Cast<Match>().Select(m => m.Value).Distinct();

            //then check each one against the hashset, only removing
            //registered emoji. This allows non-emoji versions 
            //of the pattern to survive...
            foreach (string match in distinct)
                if (_emoji.Contains(match))
                    output = output.Replace(match, string.Empty);
        }

        return output;
    }
}

public class MainClass
{
    static void Main(string[] args)
    {
        var program = new Program(new string[] { ":grinning:", ":kissing_heart:", ":bouquet:" });
        string output = program.RemoveEmoji("Hello:grinning: :imadethis:, how are you?:kissing_heart: Are you fine?:bouquet: This is:a:strange:thing :to type:, but valid :nonetheless:");
        Console.WriteLine(output);
    }
}

这导致:

你好:imadethis:,你好吗?你还好吗?这个 is:a:strange:thing :to type:, 但有效:尽管如此:

终于抽出时间写点东西了。我结合了前面提到的几个想法,事实上我们应该只循环一次字符串。根据这些要求,这听起来像是 Linq.

的完美工作

您可能应该缓存 HashSet。除此之外,它具有 O(n) 性能并且只遍历列表一次。进行基准测试会很有趣,但这很可能是最有效的解决方案。

方法非常简单。

  • 首先将所有 Emoij 加载到 HashSet 中以便我们可以快速查找它们。
  • : 处用 input.Split(':') 拆分字符串。
  • 决定是否保留当前元素。
    • 如果最后一个元素匹配,则保留当前元素。
    • 如果最后一个元素不匹配,检查当前元素是否匹配。
      • 如果是,忽略它。 (这有效地从输出中删除了子字符串)。
      • 如果没有,请追加 : 并保留它。
  • StringBuilder重建我们的字符串。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    static class Program
    {
        static void Main(string[] args)
        {
            ISet<string> emojiList = new HashSet<string>(new[] { "kissing_heart", "bouquet", "grinning" });

            Console.WriteLine("Hello:grinning: , ho:w: a::re you?:kissing_heart:kissing_heart: Are you fine?:bouquet:".RemoveEmoji(':', emojiList));
            Console.ReadLine();
        }

        public static string RemoveEmoji(this string input, char delimiter, ISet<string> emojiList)
        {
            StringBuilder sb = new StringBuilder();
            input.Split(delimiter).Aggregate(true, (prev, curr) =>
            {
                if (prev)
                {
                    sb.Append(curr);
                    return false;
                }
                if (emojiList.Contains(curr))
                {
                    return true;
                }
                sb.Append(delimiter);
                sb.Append(curr);
                return false;
            });
            return sb.ToString();
        }
    }
}

编辑:我使用 Rx library 做了一些很酷的事情,但后来意识到 Aggregate 是 Rx 中 ScanIEnumerable 对应物,从而进一步简化了代码.

如果效率是一个问题并避免处理 "false positives",请考虑使用 StringBuilder 重写字符串,同时跳过特殊的表情符号标记:

static HashSet<string> emojis = new HashSet<string>()
{
    "grinning",
    "kissing_heart",
    "bouquet"
};

static string RemoveEmojis(string input)
{
    StringBuilder sb = new StringBuilder();

    int length = input.Length;
    int startIndex = 0;
    int colonIndex = input.IndexOf(':');

    while (colonIndex >= 0 && startIndex < length)
    {
        //Keep normal text
        int substringLength = colonIndex - startIndex;
        if (substringLength > 0)
            sb.Append(input.Substring(startIndex, substringLength));

        //Advance the feed and get the next colon
        startIndex = colonIndex + 1;
        colonIndex = input.IndexOf(':', startIndex);

        if (colonIndex < 0) //No more colons, so no more emojis
        {
            //Don't forget that first colon we found
            sb.Append(':');
            //Add the rest of the text
            sb.Append(input.Substring(startIndex));
            break;
        }
        else //Possible emoji, let's check
        {
            string token = input.Substring(startIndex, colonIndex - startIndex);

            if (emojis.Contains(token)) //It's a match, so we skip this text
            {
                //Advance the feed
                startIndex = colonIndex + 1;
                colonIndex = input.IndexOf(':', startIndex);
            }
            else //No match, so we keep the normal text
            {
                //Don't forget the colon
                sb.Append(':');

                //Instead of doing another substring next loop, let's just use the one we already have
                sb.Append(token);
                startIndex = colonIndex;
            }
        }
    }

    return sb.ToString();
}

static void Main(string[] args)
{
    List<string> inputs = new List<string>()
    {
        "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:",
        "Tricky test:123:grinning:",
        "Hello:grinning: :imadethis:, how are you?:kissing_heart: Are you fine?:bouquet: This is:a:strange:thing :to type:, but valid :nonetheless:"
    };

    foreach (string input in inputs)
    {
        Console.WriteLine("In  <- " + input);
        Console.WriteLine("Out -> " + RemoveEmojis(input));
        Console.WriteLine();
    }

    Console.WriteLine("\r\n\r\nPress enter to exit...");
    Console.ReadLine();
}

输出:

In  <- Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:
Out -> Hello , how are you? Are you fine?

In  <- Tricky test:123:grinning:
Out -> Tricky test:123

In  <- Hello:grinning: :imadethis:, how are you?:kissing_heart: Are you fine?:bouquet: This is:a:strange:thing :to type:, but valid :nonetheless:
Out -> Hello :imadethis:, how are you? Are you fine? This is:a:strange:thing :to type:, but valid :nonetheless:

使用我在下面提供的这段代码我认为使用这个功能你的问题会得到解决。

        string s = "Hello:grinning: , how are you?:kissing_heart: Are you fine?:bouquet:";

        string rmv = ""; string remove = "";
        int i = 0; int k = 0;
    A:
        rmv = "";
        for (i = k; i < s.Length; i++)
        {
            if (Convert.ToString(s[i]) == ":")
            {
                for (int j = i + 1; j < s.Length; j++)
                {
                    if (Convert.ToString(s[j]) != ":")
                    {
                        rmv += s[j];
                    }
                    else
                    {
                        remove += rmv + ",";
                        i = j;
                        k = j + 1;
                        goto A;
                    }
                }
            }
        }

        string[] str = remove.Split(',');
        for (int x = 0; x < str.Length-1; x++)
        {
            s = s.Replace(Convert.ToString(":" + str[x] + ":"), "");
        }
        Console.WriteLine(s);
        Console.ReadKey();

我会使用这样的扩展方法:

public static class Helper
{
   public static string MyReplace(this string dirty, char separator)
    {
        string newText = "";
        bool replace = false;

        for (int i = 0; i < dirty.Length; i++)
        {
            if(dirty[i] == separator) { replace = !replace ; continue;}
            if(replace ) continue;
            newText += dirty[i];
        }
        return newText;
    }

}

用法:

richTextBox2.Text = richTextBox2.Text.MyReplace(':');

与使用 Regex 的方法相比,此方法在性能方面表现更好

我会用“:”拆分文本,然后构建不包括找到的表情符号名称的字符串。

        const char marker = ':';
        var textSections = text.Split(marker);

        var emojiRemovedText = string.Empty;

        var notMatchedCount = 0;
        textSections.ToList().ForEach(section =>
        {
            if (emojiNames.Contains(section))
            {
                notMatchedCount = 0;
            }
            else
            {
                if (notMatchedCount++ > 0)
                {
                    emojiRemovedText += marker.ToString();

                }
                emojiRemovedText += section;
            }
        });