【问题标题】:Regex doesn't recognize String C#正则表达式无法识别字符串 C#
【发布时间】:2018-05-24 05:08:31
【问题描述】:

我正在使用正则表达式类。我试图弄清楚,有多少常见的匹配字符串 在另一个字符串中。

情况如下:

MainWindow.DetailBLL.Name = "Top Senders By Total Threat Messages"
String detailName = MainWindow.DetailBLL.Name;

摘自:

MainWindow = Window Class
DetailBLL = Class
Name = Variable

public String Name
{
    get { return _Name; }
    set { _Name = value; }
}

CharacterReplacement(openedFile) = "Incoming Mail Domains Top Senders By Total Threat Messages"
String fileName = CharacterReplacement(openedFile); 

摘自:

OpenFileDialog openedFile = new OpenFileDialog();

Incoming_Mail_Domains_Top_Senders_by_Graymail_Messages_RawData.csv

private String CharacterReplacement(OpenFileDialog file)
{
    String input = file.SafeFileName;
    String output = input.Replace("_", " ").Replace("RawData", " ").Replace("by",      "By").Replace(".csv", " ");

    //output: "Incoming Mail Domains Top Senders By Graymail Messages"
    return output;
}

此方法获取文件的名称(.csv 文件的名称)并将其转换为可读的字符串,并按所示返回。

Regex 类的使用:

String source = detailName;

String searchPattern = fileName;

第一次尝试:不起作用

int count = Regex.Matches(searchPattern, source).Count;

或者不工作

int count = Regex.Matches(fileName, detailName).Count;

if (count > 0)
{
    System.Windows.MessageBox.Show("Match!");
}

第二次尝试:不起作用

foreach (Match match in Regex.Matches(fileName, detailName))

或者不工作

foreach (Match match in Regex.Matches(searchPattern, source))
{
    System.Windows.MessageBox.Show("Matches: " + counter++);
}

我注意到一些事情,Regex 不是这样工作的。变量没有识别:

String source = detailName;

String searchPattern = fileName;

只有当变量是这样的时候才有效:

String source = "Top Senders By Total Threat Messages";

String searchPattern = "Incoming Mail Domains Top Senders By Total Threat Messages";

但是,这对我不起作用,我需要它们评估为隐式(非文字)字符串,而不是显式(文字)字符串, 导致变量每次都发生变化。

请问有办法吗?

【问题讨论】:

  • 我认为你对正则表达式的作用感到困惑
  • 在输入字符串之前,您应该先阅读文档:msdn.microsoft.com/en-us/library/…
  • 正则表达式不用于比较两个字符串的匹配程度。
  • 如果你有正确的模式,你的第一次尝试应该会奏效。您能否更新您的问题并告诉我们您想要实现什么?我想用'|'替换空格in 模式可能会对您有所帮助,但我真的不明白您想用这个正则表达式做什么(或为什么)。例如。取问题中的最后两个字符串(source 和 searchPattern),然后告诉我们计数应该是多少以及如何实现。
  • 用户将 .csv 加载到 DataGrid。我只希望 searchPattern (传入文件)拥有源的大部分字符串。我想知道如何做到这一点,他们之间有多少场比赛。我需要检查其中至少 3 个。当然,searchPattern 会随着每次提交类似来源而发生变化。

标签: c# regex string casting pattern-matching


【解决方案1】:

嗯,首先 - 你可能不需要正则表达式(我仍然建议阅读有关正则表达式https://www.regular-expressions.info/)。

我想您需要计算两个字符串中包含多少个单词。您的 cmets 两个问题都没有说明您是想计算同一个单词两次还是只计算一次。

您可以在这里找到基本示例:

using System;
using System.Linq;

namespace SearchLinq
{
    class Program
    {
        static void Main(string[] args)
        {
            string source = "Top Senders By Total Threat Messages";
            string find = "Incoming Mail Domains Top Senders By Total Threat Messages";

            // first possible solution
            int result = 0;
            foreach (string word in find.Split(' '))
            {
                if (source.Contains(word))
                {
                    result++;
                }
            }
            Console.WriteLine(result);

            // second possible solution
            int result2 = find.Split(' ').Count(w => source.Contains(w));
            Console.WriteLine(result2);
        }
    }
}

【讨论】:

  • @MAC293 很高兴它对您有所帮助。请将此标记为答案。
猜你喜欢
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多