【问题标题】:if statements with different regular expression parameters具有不同正则表达式参数的 if 语句
【发布时间】:2017-04-19 10:18:27
【问题描述】:

您好,我正在尝试阅读单词列表上的标签,该列表中的单词如 (TMConnects(MEN) what(WP) occurred(VB) to(TO) your(ADV) my(ADV) tm(N) website(N) i(N) can(ADV) access(N) to(TO) view(N) my(ADV) bills(N) ) 我正在尝试使用 if 语句和正则表达式来读取每个标签,这样我就可以适当地对单词进行分类。

这是我试过的代码

 foreach (string word in tokensList)
        {
            //Verbs
            Match match_verb = Regex.Match(word, @"[a-zA-Z]+\(vb\)");
            if (match_verb.Success)
            {
                Console.WriteLine(word + "v");
                Verbs.Add(word);
            }
            else
            {
                //Nouns
                Match match_noun = Regex.Match(word, @"[a-zA-Z]+\(n\)");
                if (match_noun.Success)
                {
                    Console.WriteLine(word + "n");
                    Nouns.Add(word);
                }
                else
                {
                    //Adverb(Ad)
                    Match match_adverb = Regex.Match(word, @"[a-zA-Z]+\(adv\)");
                    if (match_adverb.Success)
                    {
                        Console.WriteLine(word + "adv");
                        Adverbs.Add(word);
                    }
                    else
                    {
                        //Adjective(Adj)
                        Match match_adj = Regex.Match(word, @"[a-zA-Z]+\(adj\)");
                        if (match_adj.Success)
                        {
                            Console.WriteLine(word + "adj");
                            Adjectives.Add(word);
                        }
                        else
                        {
                            //Mention(Men)
                            Match match_men = Regex.Match(word, @"[a-zA-Z]+\(men\)");
                            if (match_men.Success)
                            {
                                Console.WriteLine(word + "men");
                                Mentions.Add(word);
                            }
                            else
                            {
                                //Object(KNK)
                                Match match_obj = Regex.Match(word, @"[a-zA-Z]+\(knk\)");
                                if (match_obj.Success)
                                {
                                    Console.WriteLine(word + "obj");
                                    Objects.Add(word);
                                }
                                else
                                {
                                    //Features(KT)
                                    Match match_feature = Regex.Match(word, @"[a-zA-Z]+\(kt\)");
                                    if (match_feature.Success)
                                    {
                                        Console.WriteLine(word + "ft");
                                        Features.Add(word);
                                    }
                                    else
                                    {
                                        //break;
                                    }
                                }
                            }
                        }
                    }
                }

请帮帮我。

【问题讨论】:

  • 您期待什么帮助?我可以建议的一件事是输入 word 一次只能属于一个类别,但不能超过一个。因此,您可以使用if-else 循环而不是嵌套的if-else 循环,
  • 帮你什么?你没有告诉我们什么是行不通的。标签是大写的,就像你的例子一样?正则表达式默认区分大小写(没有RegexOptions.IgnoreCase)。似乎用一个正则表达式来提取单词和标签,然后打开标签会更干净,或者有一个字典,标签作为键,列表作为值?
  • 我正在尝试阅读单词上的标签,然后对单词进行分类,但问题似乎是某些 if 语句被简单地忽略了。例如,对于我上面给出的单词列表,它应该找到它们并打印出来,但它只找到( my(adv)adv website(n) can(adv) my(adv)

标签: c# regex vb.net


【解决方案1】:

如果我要将您提供的列表中括号内的所有文本小写,我会得到所有正确的结果。所以,我假设问题是Mark suggested:您的正则表达式区分大小写有问题,要解决这个问题,您所要做的就是提供RegexOptions.IgnoreCase 选项。

所以不是Regex.Match(word, @"[a-zA-Z]+\(vb\)") 它会是这样的Regex.Match(word, @"[a-z]+\(vb\)", RegexOptions.IgnoreCase)

但是,再次遵循 Marks 的建议,您很可能会以一种过于迎合特定解决方案的方式来解决问题。也就是说,您可以使用更通用的正则表达式并利用 .NET 提供的工具来执行单个匹配,而不是为每个条件使用 if-else 语句(尝试每次都执行正则表达式匹配)更灵活的解决方案。

    foreach (var match in
        tokensList.Select(
            word =>
                // Two capturing groups: "word" (unnecessary in your case) and "type", 
                // where "word" contains the word only (e.g. TMConnects) and "type" contains the type only (e.g. MEN).
                Regex.Match(
                    word,
                    // Match any letters appearing one or more times followed by an opening bracket ('('), anything that 
                    // isn't a closing bracket and a closing bracket (')').
                    @"^(?<word>[a-z]+)\((?<type>[^\)]+)\)$", 
                    // Tell the regular expression engine to ignore the casing.
                    RegexOptions.IgnoreCase))
                    // Filter out words that don't match the regular expression.
                    .Where(match => match.Success)) 
    {
        // The whole word (including the type.
        var word = match.Value;
        // The type lower-cased, for convenience.
        var type = match.Groups["type"].Value.ToLowerInvariant();
        switch (type)
        {
            case "vb":
                Console.WriteLine(word + "v");
                Verbs.Add(word);
                break;
            case "n":
                Console.WriteLine(word + "n");
                Nouns.Add(word);
                break;
            case "adv":
                Console.WriteLine(word + "adv");
                Adverbs.Add(word);
                break;
            case "adj":
                Console.WriteLine(word + "adj");
                Adjectives.Add(word);
                break;
            case "men":
                Console.WriteLine(word + "men");
                Mentions.Add(word);
                break;
            case "knk":
                Console.WriteLine(word + "obj");
                Objects.Add(word);
                break;
            case "kt":
                Console.WriteLine(word + "ft");
                Features.Add(word);
                break;
        }
    }

【讨论】:

  • 谢谢@eitamal,但问题仍然存在,有些词被完全忽略了,例如单词 tm(N) website(N) i(N) access(N) view(N) bills(N ) 都是名词,因此应该添加到名词列表中,但是当我在该列表中打印时它只包含单词 website,我不明白为什么它会跳过所有其他单词,循环应该遍历所有其中。
  • 看看您是否可以查明您遇到的问题。我的猜测是令牌列表与我使用的有点不同。
  • 你太棒了,这实际上帮助我解决了我的问题!
猜你喜欢
  • 1970-01-01
  • 2019-11-28
  • 2011-08-21
  • 2022-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多